Skip to content

Commit

Permalink
Add new convert subcommand to config command
Browse files Browse the repository at this point in the history
The `config convert` command can be used to convert between several different
configuration backends
  • Loading branch information
penguwin authored and muesli committed Aug 21, 2020
1 parent 6d2193d commit fd64583
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cmd/knoxite/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ var (
return executeConfigCat()
},
}
configConvertCmd = &cobra.Command{
Use: "convert <source> <target>",
Short: "convert between several configuration backends",
Long: "The convert command translates between several configuration backends",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("convert needs a source to work on")
}
if len(args) < 2 {
return fmt.Errorf("convert needs a target to write to")
}
return executeConfigConvert(args[0], args[1])
},
}
)

func init() {
Expand All @@ -81,6 +95,7 @@ func init() {
configCmd.AddCommand(configAliasCmd)
configCmd.AddCommand(configInfoCmd)
configCmd.AddCommand(configCatCmd)
configCmd.AddCommand(configConvertCmd)
RootCmd.AddCommand(configCmd)
}

Expand Down Expand Up @@ -164,3 +179,24 @@ func executeConfigCat() error {
fmt.Printf("%s\n", json)
return nil
}

func executeConfigConvert(source string, target string) error {
// Load the source config
scr, err := config.New(source)
if err != nil {
return err
}
if err = scr.Load(); err != nil {
return err
}

// Create the target
tar, err := config.New(target)
if err != nil {
return err
}

// copy over the repo configs and save the target
tar.Repositories = scr.Repositories
return tar.Save()
}

0 comments on commit fd64583

Please sign in to comment.