mirror of
https://github.com/Luzifer/ansible-role-version.git
synced 2024-12-22 18:41:22 +00:00
Migrate to cobra to support more future commands
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
a80ee0d924
commit
2db953925e
5 changed files with 119 additions and 77 deletions
9
cmd/config.go
Normal file
9
cmd/config.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package cmd
|
||||
|
||||
var (
|
||||
cfg = struct {
|
||||
RolesFile string
|
||||
}{}
|
||||
|
||||
version string
|
||||
)
|
53
cmd/helper.go
Normal file
53
cmd/helper.go
Normal file
|
@ -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
|
||||
}
|
30
cmd/root.go
Normal file
30
cmd/root.go
Normal file
|
@ -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")
|
||||
}
|
24
cmd/set.go
Normal file
24
cmd/set.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// setCmd represents the set command
|
||||
var setCmd = &cobra.Command{
|
||||
Use: "set <role> <version>",
|
||||
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)
|
||||
}
|
80
main.go
80
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 <role-name> <new 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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue