From f767609b3b70a55bcac2b3b74f61010638dba46f Mon Sep 17 00:00:00 2001 From: Pedro Tashima Date: Thu, 11 Jan 2024 19:37:09 -0300 Subject: [PATCH 1/9] add config view command --- cmd/release/cmd/config.go | 9 +++++++-- cmd/release/cmd/root.go | 22 +++++++--------------- cmd/release/config/config.go | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/cmd/release/cmd/config.go b/cmd/release/cmd/config.go index 395207a1..88ff7334 100644 --- a/cmd/release/cmd/config.go +++ b/cmd/release/cmd/config.go @@ -33,8 +33,13 @@ var viewConfigSubCmd = &cobra.Command{ Use: "view", Short: "view config", Long: ``, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Here we are!") + RunE: func(cmd *cobra.Command, args []string) error { + conf, err := rootConfig.String() + if err != nil { + return err + } + fmt.Println(conf) + return nil }, } diff --git a/cmd/release/cmd/root.go b/cmd/release/cmd/root.go index 4651bab4..3b24fa55 100644 --- a/cmd/release/cmd/root.go +++ b/cmd/release/cmd/root.go @@ -3,9 +3,9 @@ package cmd import ( "fmt" "os" - "path/filepath" "github.com/rancher/ecm-distro-tools/cmd/release/config" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -33,24 +33,16 @@ func SetVersion(version string) { } func init() { - rootCmd.Flags().BoolP("debug", "d", false, "Debug") + rootCmd.PersistentFlags().BoolP("debug", "d", false, "Debug") - homeDir, err := os.UserHomeDir() + configPath, err := config.DefaultConfigPath() if err != nil { - fmt.Println(err) - os.Exit(1) + logrus.Fatal(err) } - - const ( - ecmDistroDir = ".ecm-distro-tools" - configFileName = "config.json" - ) - configFile := filepath.Join(homeDir, ecmDistroDir, configFileName) - - conf, err := config.Load(configFile) + conf, err := config.Load(configPath) if err != nil { - fmt.Println(err) - os.Exit(1) + logrus.Fatal(err) } + rootConfig = conf } diff --git a/cmd/release/config/config.go b/cmd/release/config/config.go index 405d426c..4dd2451b 100644 --- a/cmd/release/config/config.go +++ b/cmd/release/config/config.go @@ -4,6 +4,12 @@ import ( "encoding/json" "io" "os" + "path/filepath" +) + +const ( + ecmDistroDir = ".ecm-distro-tools" + configFileName = "config.json" ) // K3sRelease @@ -57,6 +63,14 @@ type Config struct { Auth *Auth `json:"auth"` } +func DefaultConfigPath() (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", nil + } + return filepath.Join(homeDir, ecmDistroDir, configFileName), nil +} + // Load reads the given config file and returns a struct // containing the necessary values to perform a release. func Load(configFile string) (*Config, error) { @@ -74,3 +88,11 @@ func read(r io.Reader) (*Config, error) { } return &c, nil } + +func (c *Config) String() (string, error) { + b, err := json.MarshalIndent(c, "", " ") + if err != nil { + return "", err + } + return string(b), nil +} From ebe0c39c4a057264169e9b5dd0256593ce479035 Mon Sep 17 00:00:00 2001 From: Pedro Tashima Date: Fri, 12 Jan 2024 08:33:35 -0300 Subject: [PATCH 2/9] add the config edit command --- cmd/release/cmd/config.go | 5 +++-- cmd/release/config/config.go | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cmd/release/cmd/config.go b/cmd/release/cmd/config.go index 88ff7334..4a00c49c 100644 --- a/cmd/release/cmd/config.go +++ b/cmd/release/cmd/config.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/rancher/ecm-distro-tools/cmd/release/config" "github.com/spf13/cobra" ) @@ -47,8 +48,8 @@ var editConfigSubCmd = &cobra.Command{ Use: "edit", Short: "edit config", Long: ``, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Here we are!") + RunE: func(cmd *cobra.Command, args []string) error { + return config.OpenOnEditor() }, } diff --git a/cmd/release/config/config.go b/cmd/release/config/config.go index 4dd2451b..2a742e79 100644 --- a/cmd/release/config/config.go +++ b/cmd/release/config/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "io" "os" + "os/exec" "path/filepath" ) @@ -96,3 +97,22 @@ func (c *Config) String() (string, error) { } return string(b), nil } + +func OpenOnEditor() error { + confPath, err := DefaultConfigPath() + if err != nil { + return err + } + cmd := exec.Command(textEditorName(), confPath) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + return cmd.Run() +} + +func textEditorName() string { + editor := os.Getenv("EDITOR") + if editor == "" { + editor = "vi" + } + return editor +} From 5ebae5a06fc3123d6705b29b3b7f60f5e2a387c2 Mon Sep 17 00:00:00 2001 From: Pedro Tashima Date: Fri, 12 Jan 2024 15:34:17 -0300 Subject: [PATCH 3/9] add config gen --- cmd/release/cmd/config.go | 21 ++++++------- cmd/release/cmd/root.go | 6 ++++ cmd/release/config/config.go | 61 ++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 11 deletions(-) diff --git a/cmd/release/cmd/config.go b/cmd/release/cmd/config.go index 4a00c49c..c0cadaf9 100644 --- a/cmd/release/cmd/config.go +++ b/cmd/release/cmd/config.go @@ -5,6 +5,7 @@ import ( "os" "github.com/rancher/ecm-distro-tools/cmd/release/config" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -24,9 +25,15 @@ var configCmd = &cobra.Command{ var genConfigSubCmd = &cobra.Command{ Use: "gen", Short: "generate config", - Long: ``, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Here we are!") + Long: `generates a new config in the default location if it doesn't exists`, + RunE: func(cmd *cobra.Command, args []string) error { + if err := config.GenConfig(); err != nil { + return err + } + logrus.Info("config generated") + logrus.Info("to view it, run: release config view") + logrus.Info("to edit it, run: release config edit") + return nil }, } @@ -70,11 +77,3 @@ func init() { // is called directly, e.g.: // configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } - -const configViewTemplate = `RKE2 Version ------------- -{{- range .RKE2.Versions }} -{{ . -}}+rke2r1 -{{- end}} - -` diff --git a/cmd/release/cmd/root.go b/cmd/release/cmd/root.go index 3b24fa55..4fb651fb 100644 --- a/cmd/release/cmd/root.go +++ b/cmd/release/cmd/root.go @@ -39,8 +39,14 @@ func init() { if err != nil { logrus.Fatal(err) } + + if os.Args[1] == "config" && os.Args[2] == "gen" { + logrus.Info("running release config gen, skipping config load") + return + } conf, err := config.Load(configPath) if err != nil { + logrus.Error("error loading config, check if it exisits at " + configPath + " and if not, use: release config gen") logrus.Fatal(err) } diff --git a/cmd/release/config/config.go b/cmd/release/config/config.go index 2a742e79..93b8f067 100644 --- a/cmd/release/config/config.go +++ b/cmd/release/config/config.go @@ -2,10 +2,12 @@ package config import ( "encoding/json" + "errors" "io" "os" "os/exec" "path/filepath" + "strings" ) const ( @@ -109,6 +111,28 @@ func OpenOnEditor() error { return cmd.Run() } +func GenConfig() error { + var configExists = true + configPath, err := DefaultConfigPath() + if err != nil { + return err + } + if _, err := os.Stat(configPath); err != nil { + if !strings.Contains(err.Error(), "no such file or directory") { + return err + } + configExists = false + } + if configExists { + return errors.New("config already exists at " + configPath) + } + confB, err := json.MarshalIndent(exampleConfig(), "", " ") + if err != nil { + return err + } + return os.WriteFile(configPath, confB, 0644) +} + func textEditorName() string { editor := os.Getenv("EDITOR") if editor == "" { @@ -116,3 +140,40 @@ func textEditorName() string { } return editor } + +func exampleConfig() Config { + gopath := os.Getenv("GOPATH") + return Config{ + User: &User{ + Email: "your.name@suse.com", + }, + K3s: &K3s{ + Version: map[string]K3sRelease{ + "v1.x.y": { + OldK8sVersion: "v1.x.z", + NewK8sVersion: "v1.x.y", + OldK8sClient: "v0.x.z", + NewK8sClient: "v0.x.y", + OldSuffix: "k3s1", + NewSuffix: "k3s1", + ReleaseBranch: "release-1.x", + DryRun: false, + }, + }, + Workspace: filepath.Join(gopath, "src", "github.com", "k3s-io", "kubernetes"), + }, + RKE2: &RKE2{ + Versions: []string{"v1.x.y"}, + }, + Auth: &Auth{ + Drone: &Drone{ + K3sPR: "YOUR_TOKEN", + K3sPublish: "YOUR_TOKEN", + RancherPR: "YOUR_TOKEN", + RancherPublish: "YOUR_TOKEN", + }, + GithubToken: "YOUR_TOKEN", + SSHKeyPath: "path/to/your/ssh/key", + }, + } +} From 5f488920fb2e0735ccf6fd60483069794fa5b1c8 Mon Sep 17 00:00:00 2001 From: Pedro Tashima Date: Mon, 15 Jan 2024 15:56:15 -0300 Subject: [PATCH 4/9] remove logrus --- cmd/release/cmd/root.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cmd/release/cmd/root.go b/cmd/release/cmd/root.go index 4fb651fb..9b425960 100644 --- a/cmd/release/cmd/root.go +++ b/cmd/release/cmd/root.go @@ -5,7 +5,6 @@ import ( "os" "github.com/rancher/ecm-distro-tools/cmd/release/config" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -37,17 +36,19 @@ func init() { configPath, err := config.DefaultConfigPath() if err != nil { - logrus.Fatal(err) + fmt.Println(err) + os.Exit(1) } if os.Args[1] == "config" && os.Args[2] == "gen" { - logrus.Info("running release config gen, skipping config load") + fmt.Println("running release config gen, skipping config load") return } conf, err := config.Load(configPath) if err != nil { - logrus.Error("error loading config, check if it exisits at " + configPath + " and if not, use: release config gen") - logrus.Fatal(err) + fmt.Println("error loading config, check if it exisits at " + configPath + " and if not, use: release config gen") + fmt.Println(err) + os.Exit(1) } rootConfig = conf From 7c868ca95fadfa55cc8eaf28c3a08f92ef9fc10f Mon Sep 17 00:00:00 2001 From: Pedro Tashima Date: Mon, 15 Jan 2024 15:56:45 -0300 Subject: [PATCH 5/9] rename genconfig to generate --- cmd/release/cmd/config.go | 2 +- cmd/release/config/config.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/release/cmd/config.go b/cmd/release/cmd/config.go index c0cadaf9..4795fa29 100644 --- a/cmd/release/cmd/config.go +++ b/cmd/release/cmd/config.go @@ -27,7 +27,7 @@ var genConfigSubCmd = &cobra.Command{ Short: "generate config", Long: `generates a new config in the default location if it doesn't exists`, RunE: func(cmd *cobra.Command, args []string) error { - if err := config.GenConfig(); err != nil { + if err := config.Generate(); err != nil { return err } logrus.Info("config generated") diff --git a/cmd/release/config/config.go b/cmd/release/config/config.go index 93b8f067..b9740a6b 100644 --- a/cmd/release/config/config.go +++ b/cmd/release/config/config.go @@ -111,7 +111,7 @@ func OpenOnEditor() error { return cmd.Run() } -func GenConfig() error { +func Generate() error { var configExists = true configPath, err := DefaultConfigPath() if err != nil { From 44a5edaeb52f067db653f368d79ecedf2f24e26d Mon Sep 17 00:00:00 2001 From: Pedro Tashima Date: Mon, 15 Jan 2024 15:57:10 -0300 Subject: [PATCH 6/9] remove var from configexists --- cmd/release/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/release/config/config.go b/cmd/release/config/config.go index b9740a6b..40df2106 100644 --- a/cmd/release/config/config.go +++ b/cmd/release/config/config.go @@ -112,7 +112,7 @@ func OpenOnEditor() error { } func Generate() error { - var configExists = true + configExists := true configPath, err := DefaultConfigPath() if err != nil { return err From b5f2814866d9a46aa14a10d6a92ce6774d38724d Mon Sep 17 00:00:00 2001 From: Pedro Tashima Date: Mon, 15 Jan 2024 15:58:36 -0300 Subject: [PATCH 7/9] remove view command --- cmd/release/cmd/config.go | 15 +++++---------- cmd/release/config/config.go | 8 -------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/cmd/release/cmd/config.go b/cmd/release/cmd/config.go index 4795fa29..f65f0fe5 100644 --- a/cmd/release/cmd/config.go +++ b/cmd/release/cmd/config.go @@ -1,11 +1,11 @@ package cmd import ( + "errors" "fmt" "os" "github.com/rancher/ecm-distro-tools/cmd/release/config" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -30,9 +30,9 @@ var genConfigSubCmd = &cobra.Command{ if err := config.Generate(); err != nil { return err } - logrus.Info("config generated") - logrus.Info("to view it, run: release config view") - logrus.Info("to edit it, run: release config edit") + fmt.Println("config generated") + fmt.Println("to view it, run: release config view") + fmt.Println("to edit it, run: release config edit") return nil }, } @@ -42,12 +42,7 @@ var viewConfigSubCmd = &cobra.Command{ Short: "view config", Long: ``, RunE: func(cmd *cobra.Command, args []string) error { - conf, err := rootConfig.String() - if err != nil { - return err - } - fmt.Println(conf) - return nil + return errors.New("not implemented yet") }, } diff --git a/cmd/release/config/config.go b/cmd/release/config/config.go index 40df2106..c1eea5ac 100644 --- a/cmd/release/config/config.go +++ b/cmd/release/config/config.go @@ -92,14 +92,6 @@ func read(r io.Reader) (*Config, error) { return &c, nil } -func (c *Config) String() (string, error) { - b, err := json.MarshalIndent(c, "", " ") - if err != nil { - return "", err - } - return string(b), nil -} - func OpenOnEditor() error { confPath, err := DefaultConfigPath() if err != nil { From 1eda3975b89448d8c246bd526f5a13fa63baefd5 Mon Sep 17 00:00:00 2001 From: Pedro Tashima Date: Wed, 17 Jan 2024 16:13:30 -0300 Subject: [PATCH 8/9] re add config view template --- cmd/release/config/config.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cmd/release/config/config.go b/cmd/release/config/config.go index c1eea5ac..f483ac27 100644 --- a/cmd/release/config/config.go +++ b/cmd/release/config/config.go @@ -169,3 +169,10 @@ func exampleConfig() Config { }, } } + +const configViewTemplate = `RKE2 Version +------------ +{{- range .RKE2.Versions }} +{{ . -}}+rke2r1 +{{- end}} +` From 5cb649d0f8ff8e868f7518f3b750deb6069d06ff Mon Sep 17 00:00:00 2001 From: Pedro Tashima Date: Tue, 23 Jan 2024 14:03:54 -0300 Subject: [PATCH 9/9] update failed to load config message --- cmd/release/cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/release/cmd/root.go b/cmd/release/cmd/root.go index 9b425960..92cd4d74 100644 --- a/cmd/release/cmd/root.go +++ b/cmd/release/cmd/root.go @@ -46,7 +46,7 @@ func init() { } conf, err := config.Load(configPath) if err != nil { - fmt.Println("error loading config, check if it exisits at " + configPath + " and if not, use: release config gen") + fmt.Println("failed to load config, use 'release config gen' to create a new one at: " + configPath) fmt.Println(err) os.Exit(1) }