Skip to content

Commit 80584a0

Browse files
author
Daniele Rondina
committed
Begin development of build-index command
0 parents  commit 80584a0

File tree

8 files changed

+514
-0
lines changed

8 files changed

+514
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.swp
2+
simplestreams-builder

builder.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
3+
Copyright (C) 2019 Daniele Rondina <[email protected]>
4+
Credits goes also to Gogs authors, some code portions and re-implemented design
5+
are also coming from the Gogs project, which is using the go-macaron framework
6+
and was really source of ispiration. Kudos to them!
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation, either version 3 of the License, or
11+
(at your option) any later version.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
21+
*/
22+
package main
23+
24+
import (
25+
cmd "github.com/MottainaiCI/simplestreams-builder/cmd"
26+
)
27+
28+
func main() {
29+
cmd.Execute()
30+
}

cmd/index.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
3+
Copyright (C) 2019 Daniele Rondina <[email protected]>
4+
Credits goes also to Gogs authors, some code portions and re-implemented design
5+
are also coming from the Gogs project, which is using the go-macaron framework
6+
and was really source of ispiration. Kudos to them!
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation, either version 3 of the License, or
11+
(at your option) any later version.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
21+
*/
22+
package cmd
23+
24+
import (
25+
"os"
26+
27+
utils "github.com/MottainaiCI/mottainai-server/pkg/utils"
28+
"github.com/spf13/cobra"
29+
30+
config "github.com/MottainaiCI/simplestreams-builder/pkg/config"
31+
index "github.com/MottainaiCI/simplestreams-builder/pkg/index"
32+
)
33+
34+
func newBuildIndexCommand(config *config.BuilderTreeConfig) *cobra.Command {
35+
var cmd = &cobra.Command{
36+
Use: "build-index",
37+
Short: "Build index.json file of the tree",
38+
Args: cobra.NoArgs,
39+
PreRun: func(cmd *cobra.Command, args []string) {
40+
},
41+
Run: func(cmd *cobra.Command, args []string) {
42+
idx, err := index.BuildIndexStruct(config)
43+
utils.CheckError(err)
44+
45+
if config.Viper.GetBool("stdout") {
46+
index.WriteIndexJson(idx, os.Stdout)
47+
}
48+
},
49+
}
50+
51+
var pflags = cmd.PersistentFlags()
52+
pflags.Bool("stdout", false, "Print index.json to stdout")
53+
config.Viper.BindPFlag("stdout", pflags.Lookup("stdout"))
54+
55+
return cmd
56+
}

cmd/print.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
3+
Copyright (C) 2019 Daniele Rondina <[email protected]>
4+
Credits goes also to Gogs authors, some code portions and re-implemented design
5+
are also coming from the Gogs project, which is using the go-macaron framework
6+
and was really source of ispiration. Kudos to them!
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation, either version 3 of the License, or
11+
(at your option) any later version.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
21+
*/
22+
package cmd
23+
24+
import (
25+
"fmt"
26+
27+
"github.com/spf13/cobra"
28+
29+
config "github.com/MottainaiCI/simplestreams-builder/pkg/config"
30+
)
31+
32+
func newPrintCommand(config *config.BuilderTreeConfig) *cobra.Command {
33+
var cmd = &cobra.Command{
34+
Use: "print",
35+
Short: "Show configuration params",
36+
Args: cobra.NoArgs,
37+
Run: func(cmd *cobra.Command, args []string) {
38+
39+
fmt.Println("===================================")
40+
fmt.Println("CONFIGURATION PARAMS:")
41+
fmt.Println("===================================")
42+
fmt.Println(config)
43+
fmt.Println("===================================")
44+
fmt.Println("")
45+
fmt.Println("===================================")
46+
fmt.Println("Config File: ", config.Viper.Get("config"))
47+
fmt.Println("===================================")
48+
},
49+
}
50+
51+
return cmd
52+
}

cmd/root.go

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
3+
Copyright (C) 2019 Daniele Rondina <[email protected]>
4+
Credits goes also to Gogs authors, some code portions and re-implemented design
5+
are also coming from the Gogs project, which is using the go-macaron framework
6+
and was really source of ispiration. Kudos to them!
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation, either version 3 of the License, or
11+
(at your option) any later version.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
21+
*/
22+
package cmd
23+
24+
import (
25+
"fmt"
26+
"os"
27+
28+
utils "github.com/MottainaiCI/mottainai-server/pkg/utils"
29+
"github.com/spf13/cobra"
30+
"github.com/spf13/viper"
31+
32+
config "github.com/MottainaiCI/simplestreams-builder/pkg/config"
33+
)
34+
35+
const (
36+
cliName = `Simplestreams Builder
37+
Copyright (c) 2019 Mottainai
38+
39+
Mottainai - LXC/LXD Simplestreams Tree Builder`
40+
41+
SSB_ENV_PREFIX = `SSBUILDER`
42+
SSB_VERSION = `0.1.0`
43+
)
44+
45+
func initConfig(config *config.BuilderTreeConfig) {
46+
// Set env variable
47+
config.Viper.SetEnvPrefix(SSB_ENV_PREFIX)
48+
config.Viper.BindEnv("config")
49+
config.Viper.SetDefault("config", "")
50+
51+
config.Viper.AutomaticEnv()
52+
53+
config.Viper.SetTypeByDefaultValue(true)
54+
}
55+
56+
func initCommand(rootCmd *cobra.Command, config *config.BuilderTreeConfig) {
57+
var pflags = rootCmd.PersistentFlags()
58+
59+
pflags.StringP("config", "c", "", "SimpleStreams Builder configuration file")
60+
pflags.StringP("target-dir", "t", "", "Target dir of operations.")
61+
62+
config.Viper.BindPFlag("config", pflags.Lookup("config"))
63+
config.Viper.BindPFlag("target-dir", pflags.Lookup("target-dir"))
64+
65+
rootCmd.AddCommand(
66+
newPrintCommand(config),
67+
newBuildIndexCommand(config),
68+
)
69+
}
70+
71+
func Execute() {
72+
// Create Main Instance Config object
73+
var config *config.BuilderTreeConfig = config.NewBuilderTreeConfig(nil)
74+
75+
initConfig(config)
76+
77+
var rootCmd = &cobra.Command{
78+
Short: cliName,
79+
Version: SSB_VERSION,
80+
Args: cobra.OnlyValidArgs,
81+
SilenceUsage: true,
82+
PreRun: func(cmd *cobra.Command, args []string) {
83+
if len(args) == 0 {
84+
cmd.Help()
85+
os.Exit(0)
86+
}
87+
},
88+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
89+
var err error
90+
var v *viper.Viper = config.Viper
91+
92+
if v.Get("config") == "" {
93+
fmt.Println("Missing configuration file")
94+
os.Exit(1)
95+
}
96+
97+
v.SetConfigType("yml")
98+
v.SetConfigFile(v.Get("config").(string))
99+
100+
// Parse configuration file
101+
err = config.Unmarshal()
102+
utils.CheckError(err)
103+
},
104+
}
105+
106+
initCommand(rootCmd, config)
107+
108+
// Start command execution
109+
if err := rootCmd.Execute(); err != nil {
110+
fmt.Println(err)
111+
os.Exit(1)
112+
}
113+
114+
}

contrib/config/tree.example.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
# Configuration script for SimpleStream Builder
3+
4+
# Define a specific path where build simple streams files.
5+
# By defualt use path / for expose images.
6+
# prefix: '/'
7+
8+
# Path of the images.json
9+
images_path: streams/v1
10+
11+
# Normally this field must be with this values for LXC.
12+
datatype: image-downloads
13+
14+
# Define version of the products.
15+
format: "products:1.0"
16+
17+
# Define list of products
18+
products:
19+
20+
# Sabayon Builder rootfs
21+
- name: sabayon-builder:current:amd64:default
22+
arch: amd64
23+
release: current
24+
os: Sabayon
25+
release_title: "Sabayon Builder"
26+
directory: sbi/sabayon-builder
27+
# Path where retrieve images informations and files
28+
#prefix_path: ""
29+
30+
aliases:
31+
- "sabayon/builder"
32+
33+
# Sabayon Base rootfs
34+
- name: sabayon-base:current:amd64:default
35+
arch: amd64
36+
release: current
37+
directory: sbi/sabayon-base
38+
aliases:
39+
- "sabayon/base"
40+

0 commit comments

Comments
 (0)