Skip to content

Commit

Permalink
add modules update command (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
aatarasoff authored Nov 20, 2023
1 parent 945b81c commit 96bb9b0
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 23 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 40 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand Down
57 changes: 42 additions & 15 deletions internal/model/model.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions pbuf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
out: third_party/google/api

0 comments on commit 96bb9b0

Please sign in to comment.