diff --git a/README.md b/README.md index 6046e7d..11dd0d3 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,14 @@ pbuf-cli modules push [tag] Replace `[tag]` with the tag you want to push. +#### Update Modules Tags + +The update command allows you to update the modules' tags to the latest in the registry. The command saves the latest tags in the `pbuf.yaml` file for each module. + +```bash +pbuf-cli modules update +``` + #### Delete Tag The delete command allows you to delete a tag from the registry. diff --git a/cmd/root.go b/cmd/root.go index 7f85363..11b88a2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,14 +19,13 @@ func NewRootCmd() *cobra.Command { Use: "pbuf-cli", Short: "PowerBuf CLI", Long: "PowerBuf CLI is a command line interface for PowerBuf", - Run: func(cmd *cobra.Command, args []string) { - // do nothing + RunE: func(cmd *cobra.Command, args []string) error { + return cmd.Help() }, } - const modulesConfigFilename = "pbuf.yaml" - // read the file (modulesConfigFilename) and call ModulesConfig.Vendor() - file, err := os.ReadFile(modulesConfigFilename) + // read the file (PbufConfigFilename) and call ModulesConfig.Vendor() + file, err := os.ReadFile(model.PbufConfigFilename) if err != nil { log.Fatalf("failed to read file: %v", err) } @@ -67,6 +66,8 @@ func NewModuleCmd(config *model.Config, client v1.RegistryClient) *cobra.Command moduleCmd.AddCommand(NewListModulesCmd(config, client)) moduleCmd.AddCommand(NewGetModuleCmd(config, client)) + moduleCmd.AddCommand(NewModuleUpdateCmd(config, client)) + return moduleCmd } @@ -279,6 +280,40 @@ func NewListModulesCmd(config *model.Config, client v1.RegistryClient) *cobra.Co return listModulesCmd } +func NewModuleUpdateCmd(config *model.Config, client v1.RegistryClient) *cobra.Command { + // create module update command + moduleUpdateCmd := &cobra.Command{ + Use: "update", + Short: "Update", + Long: "Update is a command to update modules tags to the latest ones", + Args: cobra.ExactArgs(0), + Run: func(cmd *cobra.Command, args []string) { + for _, module := range config.Modules { + if module.Name != "" && module.Repository == "" { + moduleWithTags, err := client.GetModule(cmd.Context(), &v1.GetModuleRequest{ + Name: module.Name, + }) + + if err != nil { + log.Fatalf("failed to get module: %v", err) + } + + if len(moduleWithTags.Tags) > 0 { + module.Tag = moduleWithTags.Tags[0] + } + } + } + + err := config.Save() + if err != nil { + log.Fatalf("failed to update config. error during saving: %v", err) + } + }, + } + + return moduleUpdateCmd +} + // NewVendorCmd creates cobra command for vendor func NewVendorCmd(modulesConfig *model.Config, client v1.RegistryClient) *cobra.Command { // create vendor command diff --git a/go.mod b/go.mod index 7bd9a72..e8a2f1c 100644 --- a/go.mod +++ b/go.mod @@ -8,10 +8,12 @@ require ( github.com/jdx/go-netrc v1.0.0 github.com/spf13/cobra v1.7.0 github.com/yoheimuta/go-protoparser/v4 v4.9.0 + golang.org/x/mod v0.12.0 google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 google.golang.org/grpc v1.59.0 google.golang.org/protobuf v1.31.0 gopkg.in/yaml.v2 v2.2.8 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -34,7 +36,6 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect golang.org/x/crypto v0.14.0 // indirect - golang.org/x/mod v0.12.0 // indirect golang.org/x/net v0.17.0 // indirect golang.org/x/sys v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect diff --git a/internal/model/model.go b/internal/model/model.go index ea19e6f..f3a5dff 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -1,28 +1,55 @@ package model +import ( + "os" + + "gopkg.in/yaml.v3" +) + +const ( + PbufConfigFilename = "pbuf.yaml" +) + type Config struct { - Version string `yaml:"version"` - Name string `yaml:"name"` + Version string `yaml:"version,omitempty"` + Name string `yaml:"name,omitempty"` Registry struct { - Addr string `yaml:"addr"` - Insecure bool `yaml:"insecure"` - } `yaml:"registry"` + Addr string `yaml:"addr,omitempty"` + Insecure bool `yaml:"insecure,omitempty"` + } `yaml:"registry,omitempty"` Export struct { - Paths []string `yaml:"paths"` - } `yaml:"export"` - Modules []*Module `yaml:"modules"` + Paths []string `yaml:"paths,omitempty"` + } `yaml:"export,omitempty"` + Modules []*Module `yaml:"modules,omitempty"` } type Module struct { - Name string `yaml:"name"` - Repository string `yaml:"repository"` - Path string `yaml:"path"` - Branch string `yaml:"branch"` - Tag string `yaml:"tag"` - OutputFolder string `yaml:"out"` - GenerateOutputFolder string `yaml:"gen_out"` + Name string `yaml:"name,omitempty"` + Repository string `yaml:"repository,omitempty"` + Path string `yaml:"path,omitempty"` + Branch string `yaml:"branch,omitempty"` + Tag string `yaml:"tag,omitempty"` + OutputFolder string `yaml:"out,omitempty"` + GenerateOutputFolder string `yaml:"gen_out,omitempty"` } func (c *Config) HasRegistry() bool { return c.Registry.Addr != "" } + +func (c *Config) Save() error { + // encode to yaml and save to file PbufConfigFilename + pbufYamlFile, err := os.OpenFile(PbufConfigFilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + + encoder := yaml.NewEncoder(pbufYamlFile) + encoder.SetIndent(2) + err = encoder.Encode(c) + if err != nil { + return err + } + + return nil +} diff --git a/pbuf.yaml b/pbuf.yaml index c1de6df..9e7e5d6 100644 --- a/pbuf.yaml +++ b/pbuf.yaml @@ -5,12 +5,12 @@ registry: insecure: true modules: - name: pbufio/pbuf-registry - tag: v0.2.0 path: api/pbuf-registry + tag: v0.2.0 out: third_party/pbuf-registry gen_out: gen/pbuf-registry - name: googleapis repository: https://github.com/googleapis/googleapis path: google/api branch: master - out: third_party/google/api \ No newline at end of file + out: third_party/google/api