Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#338: add config gen #340

Merged
merged 9 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions cmd/release/cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"errors"
"fmt"
"os"

"github.com/rancher/ecm-distro-tools/cmd/release/config"
"github.com/spf13/cobra"
)

Expand All @@ -23,27 +25,33 @@ 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.Generate(); err != nil {
return err
}
fmt.Println("config generated")
fmt.Println("to view it, run: release config view")
fmt.Println("to edit it, run: release config edit")
return nil
},
}

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 {
return errors.New("not implemented yet")
},
}

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()
},
}

Expand All @@ -64,11 +72,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}}

`
19 changes: 9 additions & 10 deletions cmd/release/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

"github.com/rancher/ecm-distro-tools/cmd/release/config"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -33,24 +32,24 @@ 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)
}

const (
ecmDistroDir = ".ecm-distro-tools"
configFileName = "config.json"
)
configFile := filepath.Join(homeDir, ecmDistroDir, configFileName)

conf, err := config.Load(configFile)
if os.Args[1] == "config" && os.Args[2] == "gen" {
fmt.Println("running release config gen, skipping config load")
return
}
conf, err := config.Load(configPath)
if err != nil {
fmt.Println("failed to load config, use 'release config gen' to create a new one at: " + configPath)
fmt.Println(err)
os.Exit(1)
}

rootConfig = conf
}
102 changes: 102 additions & 0 deletions cmd/release/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ package config

import (
"encoding/json"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)

const (
ecmDistroDir = ".ecm-distro-tools"
configFileName = "config.json"
)

// K3sRelease
Expand Down Expand Up @@ -57,6 +66,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) {
Expand All @@ -74,3 +91,88 @@ func read(r io.Reader) (*Config, error) {
}
return &c, 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 Generate() error {
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 == "" {
editor = "vi"
}
return editor
}

func exampleConfig() Config {
gopath := os.Getenv("GOPATH")
return Config{
User: &User{
Email: "[email protected]",
},
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",
},
}
}

const configViewTemplate = `RKE2 Version
------------
{{- range .RKE2.Versions }}
{{ . -}}+rke2r1
{{- end}}
`