Skip to content

Commit

Permalink
Add tags to module publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
kajogo777 committed Apr 23, 2023
1 parent 64447d9 commit dea6caa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
3 changes: 3 additions & 0 deletions cmd/devx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
verbosity string
stdout bool
reserve bool
tags []string
)
var server = auth.ServerConfig{}

Expand Down Expand Up @@ -65,6 +66,8 @@ func init() {
runCmd.PersistentFlags().BoolVar(&runFlags.Color, "color", true, "colored output. Enabled by default. Set flag to false to disable")
runCmd.PersistentFlags().DurationVar(&runFlags.Interval, "interval", 0, "interval to watch for changes")

publishModuleCmd.PersistentFlags().StringArrayVarP(&tags, "tag", "t", []string{}, "tags to publish the module with")

rootCmd.AddCommand(
buildCmd,
projectCmd,
Expand Down
2 changes: 1 addition & 1 deletion cmd/devx/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ var publishModuleCmd = &cobra.Command{
Use: "mod",
Short: "Publish this module",
RunE: func(cmd *cobra.Command, args []string) error {
if err := catalog.PublishModule(gitDir, configDir, server); err != nil {
if err := catalog.PublishModule(gitDir, configDir, server, tags); err != nil {
return err
}
return nil
Expand Down
45 changes: 29 additions & 16 deletions pkg/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"cuelang.org/go/cue"
"golang.org/x/mod/semver"

"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/format"
Expand Down Expand Up @@ -36,26 +37,41 @@ type ModuleItem struct {
Dependencies map[string]ModuleDependency `json:"dependencies"`
Package string `json:"package"`
Source map[string]string `json:"source"`
Git Git `json:"git"`
Tags []string `json:"tags"`
}
type ModuleDependency struct {
V *string `json:"v,omitempty"`
}

func PublishModule(gitDir string, configDir string, server auth.ServerConfig) error {
projectGitData, err := gitrepo.GetProjectGitData(gitDir)
if err != nil {
return nil
}
if projectGitData == nil {
return fmt.Errorf("git is not initialized, cannot publish a catalog without version control")
}
func PublishModule(gitDir string, configDir string, server auth.ServerConfig, tags []string) error {
gitData, err := gitrepo.GetGitData(gitDir)
if err != nil {
return nil
}

tagsToPush := []string{}

if gitData == nil {
return fmt.Errorf("git is not initialized, cannot publish a catalog without version control")
for _, gitTag := range gitData.Tags {
exists := false
for _, tag := range tags {
if tag == gitTag {
exists = true
break
}
}
if exists {
continue
}
tagsToPush = append(tagsToPush, gitTag)
}
}

for _, tag := range tags {
if !semver.IsValid(tag) {
return fmt.Errorf("invalid tag \"%s\" that is not a valid semantic version, please check https://semver.org/", tag)
}
tagsToPush = append(tagsToPush, tag)
}

moduleFilePath := filepath.Join(configDir, "cue.mod", "module.cue")
Expand Down Expand Up @@ -107,10 +123,7 @@ func PublishModule(gitDir string, configDir string, server auth.ServerConfig) er
Package: moduleName,
Dependencies: deps,
Source: overlay,
Git: Git{
*projectGitData,
*gitData,
},
Tags: tagsToPush,
}
err = publishModule(server, &item)
if err != nil {
Expand Down Expand Up @@ -336,8 +349,8 @@ func publishModule(server auth.ServerConfig, item *ModuleItem) error {
return err
}

if len(item.Git.Tags) > 0 {
log.Infof("📦 Published module %s@%s successfully", item.Module, item.Git.Tags[0])
if len(item.Tags) > 0 {
log.Infof("📦 Published module %s@%s successfully", item.Module, item.Tags[0])
} else {
log.Infof("📦 Published module %s successfully", item.Module)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ func Update(configDir string, server auth.ServerConfig) error {
}

installedVersion := "<untagged>"
if len(packageItem.Git.Tags) > 0 {
installedVersion = packageItem.Git.Tags[0]
if len(packageItem.Tags) > 0 {
installedVersion = packageItem.Tags[0]
}

log.Infof("📦 Installing %s@%s", name, installedVersion)
Expand Down

0 comments on commit dea6caa

Please sign in to comment.