From 2db953925ea69f1b04842a3f68e4fd0d011a0978 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Thu, 11 Jan 2018 00:02:48 +0100 Subject: [PATCH] Migrate to cobra to support more future commands Signed-off-by: Knut Ahlers --- cmd/config.go | 9 ++++++ cmd/helper.go | 53 ++++++++++++++++++++++++++++++++++ cmd/root.go | 30 +++++++++++++++++++ cmd/set.go | 24 ++++++++++++++++ main.go | 80 ++------------------------------------------------- 5 files changed, 119 insertions(+), 77 deletions(-) create mode 100644 cmd/config.go create mode 100644 cmd/helper.go create mode 100644 cmd/root.go create mode 100644 cmd/set.go diff --git a/cmd/config.go b/cmd/config.go new file mode 100644 index 0000000..3265b1a --- /dev/null +++ b/cmd/config.go @@ -0,0 +1,9 @@ +package cmd + +var ( + cfg = struct { + RolesFile string + }{} + + version string +) diff --git a/cmd/helper.go b/cmd/helper.go new file mode 100644 index 0000000..6a23ab2 --- /dev/null +++ b/cmd/helper.go @@ -0,0 +1,53 @@ +package cmd + +import ( + "bytes" + "fmt" + "io/ioutil" + + yaml "gopkg.in/yaml.v2" +) + +type ansibleRoleDefinition struct { + Name string `yaml:"name"` + Src string `yaml:"src"` + Version string `yaml:"version"` +} + +func patchRoleFile(rolesFile string, updates map[string]string) error { + var ( + inFileContent []byte + err error + ) + if inFileContent, err = ioutil.ReadFile(rolesFile); err != nil { + return fmt.Errorf("Roles file not found: %s", err) + } + + in := []ansibleRoleDefinition{} + if err = yaml.Unmarshal(inFileContent, &in); err != nil { + return fmt.Errorf("Unable to parse roles file: %s", err) + } + + for roleName, roleVersion := range updates { + for i := range in { + if in[i].Name == roleName { + in[i].Version = roleVersion + } + } + } + + if inFileContent, err = yaml.Marshal(in); err != nil { + return fmt.Errorf("Unable to marshal roles file: %s", err) + } + + buf := new(bytes.Buffer) + buf.Write([]byte("---\n\n")) + buf.Write(inFileContent) + buf.Write([]byte("\n...\n")) + + if err = ioutil.WriteFile(rolesFile, buf.Bytes(), 0644); err != nil { + return fmt.Errorf("Unable to write roles file: %s", err) + } + + return nil +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..15816b3 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +var cfgFile string + +// RootCmd represents the base command when called without any subcommands +var RootCmd = &cobra.Command{ + Use: "ansible-role-version", + Short: "Small tool to update a requirements.yml file for Ansible Galaxy", +} + +// Execute adds all child commands to the root command sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute(appVersion string) { + version = appVersion + if err := RootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(-1) + } +} + +func init() { + RootCmd.PersistentFlags().StringVarP(&cfg.RolesFile, "roles-file", "f", "requirements.yml", "File containing the requirements") +} diff --git a/cmd/set.go b/cmd/set.go new file mode 100644 index 0000000..29b2194 --- /dev/null +++ b/cmd/set.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// setCmd represents the set command +var setCmd = &cobra.Command{ + Use: "set ", + Short: "Set the version of a role statically", + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) != 2 { + return fmt.Errorf("You must specify role and version") + } + + return patchRoleFile(cfg.RolesFile, map[string]string{args[0]: args[1]}) + }, +} + +func init() { + RootCmd.AddCommand(setCmd) +} diff --git a/main.go b/main.go index 2ad15ad..433d6a2 100644 --- a/main.go +++ b/main.go @@ -1,83 +1,9 @@ package main -import ( - "bytes" - "fmt" - "io/ioutil" - "os" +import "github.com/Luzifer/ansible-role-version/cmd" - yaml "gopkg.in/yaml.v2" - - "github.com/Luzifer/rconfig" - log "github.com/sirupsen/logrus" -) - -var ( - cfg = struct { - RolesFile string `flag:"roles-file,f" default:"requirements.yml" description:"File containing the requirements"` - VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` - }{} - - version = "dev" -) - -type ansibleRoleDefinition struct { - Name string `yaml:"name"` - Src string `yaml:"src"` - Version string `yaml:"version"` -} - -func init() { - if err := rconfig.Parse(&cfg); err != nil { - log.Fatalf("Unable to parse commandline options: %s", err) - } - - if cfg.VersionAndExit { - fmt.Printf("ansible-role-version %s\n", version) - os.Exit(0) - } -} +var version = "dev" func main() { - args := rconfig.Args()[1:] - if len(args) != 2 { - log.Fatalf("Usage: ansible-role-version ") - } - - roleName := args[0] - roleVersion := args[1] - - var ( - inFileContent []byte - err error - ) - if inFileContent, err = ioutil.ReadFile(cfg.RolesFile); err != nil { - log.WithError(err).Fatalf("Roles file not found") - } - - in := []ansibleRoleDefinition{} - if err = yaml.Unmarshal(inFileContent, &in); err != nil { - log.WithError(err).Fatal("Unable to parse roles file") - } - - for i := range in { - if in[i].Name == roleName { - in[i].Version = roleVersion - } - } - - if inFileContent, err = yaml.Marshal(in); err != nil { - log.WithError(err).Fatal("Unable to marshal roles file") - } - - buf := new(bytes.Buffer) - buf.Write([]byte("---\n\n")) - buf.Write(inFileContent) - buf.Write([]byte("\n...\n")) - - if err = ioutil.WriteFile(cfg.RolesFile, buf.Bytes(), 0644); err != nil { - log.WithError(err).Fatal("Unable to write roles file") - } - - log.Info("Roles file written successfully") + cmd.Execute(version) }