Skip to content

Commit

Permalink
Merge pull request #335 from GoogleCloudPlatform/issue330_2
Browse files Browse the repository at this point in the history
feat: adds latest flag to scaffold #330
  • Loading branch information
srinandan authored Jan 2, 2025
2 parents a7e2335 + eefc15d commit c709632
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 20 deletions.
52 changes: 38 additions & 14 deletions internal/cmd/integrations/getversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package integrations

import (
"errors"
"fmt"
"internal/apiclient"
"internal/client/integrations"
"strconv"
Expand All @@ -34,9 +35,12 @@ var GetVerCmd = &cobra.Command{
version := cmd.Flag("ver").Value.String()
userLabel := cmd.Flag("user-label").Value.String()
snapshot := cmd.Flag("snapshot").Value.String()
latest, _ := strconv.ParseBool(cmd.Flag("latest").Value.String())

if err = apiclient.SetRegion(cmdRegion.Value.String()); err != nil {
return err
} else if err = validate(version, userLabel, snapshot, latest); err != nil {
return err
}

minimal, _ := strconv.ParseBool(cmd.Flag("minimal").Value.String())
Expand All @@ -48,22 +52,10 @@ var GetVerCmd = &cobra.Command{
return errors.New("config-vars cannot be combined with overrides, minimal or basic")
}

if snapshot == "" && userLabel == "" && version == "" {
return errors.New("at least one of snapshot, userLabel and version must be supplied")
}
if snapshot != "" && (userLabel != "" || version != "") {
return errors.New("snapshot cannot be combined with userLabel or version")
}
if userLabel != "" && (snapshot != "" || version != "") {
return errors.New("userLabel cannot be combined with snapshot or version")
}
if version != "" && (snapshot != "" || userLabel != "") {
return errors.New("version cannot be combined with snapshot or version")
}
return apiclient.SetProjectID(cmdProject.Value.String())
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
var integrationBody, respBody []byte
var integrationBody, respBody, listBody []byte
version := cmd.Flag("ver").Value.String()
name := cmd.Flag("name").Value.String()
minimal, _ := strconv.ParseBool(cmd.Flag("minimal").Value.String())
Expand All @@ -77,13 +69,41 @@ var GetVerCmd = &cobra.Command{
apiclient.DisableCmdPrintHttpResponse()
}

latest := ignoreLatest(version, userLabel, snapshot)
if latest {
// list integration versions, order by state=ACTIVE, page size = 1 and return basic info
if listBody, err = integrations.ListVersions(name, 1, "", "state=ACTIVE",
"snapshot_number", false, false, true); err != nil {
return fmt.Errorf("unable to list versions: %v", err)
}
if string(listBody) != "{}" {
if version, err = getIntegrationVersion(listBody); err != nil {
return err
}
} else {
// list integration versions, order by state=SNAPSHOT, page size = 1 and return basic info
if listBody, err = integrations.ListVersions(name, 1, "", "state=SNAPSHOT",
"snapshot_number", false, false, true); err != nil {
return fmt.Errorf("unable to list versions: %v", err)
}
if string(listBody) != "{}" {
if version, err = getIntegrationVersion(listBody); err != nil {
return err
}
}
}
}

if version != "" {
integrationBody, err = integrations.Get(name, version, basic, minimal, overrides)
} else if snapshot != "" {
integrationBody, err = integrations.GetBySnapshot(name, snapshot, basic, minimal, overrides)
} else {
} else if userLabel != "" {
integrationBody, err = integrations.GetByUserlabel(name, userLabel, basic, minimal, overrides)
} else {
return errors.New("latest version not found. Must pass oneOf version, snapshot or user-label or fix the integration name")
}

if err != nil {
return err
}
Expand All @@ -106,6 +126,7 @@ var GetVerCmd = &cobra.Command{
func init() {
var name, userLabel, snapshot, version string
minimal, overrides, basic, configVar := false, false, false, false
latest := true

GetVerCmd.Flags().StringVarP(&name, "name", "n",
"", "Integration flow name")
Expand All @@ -123,5 +144,8 @@ func init() {
false, "fields of the Integration to be returned; default is false")
GetVerCmd.Flags().BoolVarP(&configVar, "config-vars", "",
false, "Returns config variables for the integration")
GetVerCmd.Flags().BoolVarP(&latest, "latest", "",
true, "Get the integeration version in ACTIVE state, if not found the highest snapshot in SNAPSHOT state; default is true")

_ = GetVerCmd.MarkFlagRequired("name")
}
41 changes: 35 additions & 6 deletions internal/cmd/integrations/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"internal/cmd/utils"
"os"
"path"
"strconv"

"github.com/spf13/cobra"
)
Expand All @@ -42,22 +43,19 @@ var ScaffoldCmd = &cobra.Command{
version := cmd.Flag("ver").Value.String()
userLabel := cmd.Flag("user-label").Value.String()
snapshot := cmd.Flag("snapshot").Value.String()
latest, _ := strconv.ParseBool(cmd.Flag("latest").Value.String())

if err = apiclient.SetRegion(cmdRegion.Value.String()); err != nil {
return err
}
if userLabel == "" && version == "" && snapshot == "" {
return errors.New("at least one of userLabel, version or snapshot must be passed")
}
if err = validate(version, userLabel, snapshot, false); err != nil {
} else if err = validate(version, userLabel, snapshot, latest); err != nil {
return err
}
return apiclient.SetProjectID(cmdProject.Value.String())
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
const jsonExt = ".json"
var fileSplitter string
var integrationBody, overridesBody []byte
var integrationBody, overridesBody, listBody []byte
version := cmd.Flag("ver").Value.String()
userLabel := cmd.Flag("user-label").Value.String()
snapshot := cmd.Flag("snapshot").Value.String()
Expand Down Expand Up @@ -89,6 +87,32 @@ var ScaffoldCmd = &cobra.Command{
}
}

latest := ignoreLatest(version, userLabel, snapshot)

if latest {
// list integration versions, order by state=ACTIVE, page size = 1 and return basic info
if listBody, err = integrations.ListVersions(name, 1, "", "state=ACTIVE",
"snapshot_number", false, false, true); err != nil {
return fmt.Errorf("unable to list versions: %v", err)
}
if string(listBody) != "{}" {
if version, err = getIntegrationVersion(listBody); err != nil {
return err
}
} else {
// list integration versions, order by state=SNAPSHOT, page size = 1 and return basic info
if listBody, err = integrations.ListVersions(name, 1, "", "state=SNAPSHOT",
"snapshot_number", false, false, true); err != nil {
return fmt.Errorf("unable to list versions: %v", err)
}
if string(listBody) != "{}" {
if version, err = getIntegrationVersion(listBody); err != nil {
return err
}
}
}
}

// Get

if version != "" {
Expand All @@ -112,6 +136,8 @@ var ScaffoldCmd = &cobra.Command{
if overridesBody, err = integrations.GetBySnapshot(name, snapshot, false, false, true); err != nil {
return err
}
} else {
return errors.New("latest version not found. Must pass oneOf version, snapshot or user-label or fix the integration name")
}

clilog.Info.Printf("Storing the Integration: %s\n", name)
Expand Down Expand Up @@ -383,6 +409,7 @@ var (

func init() {
var name, userLabel, snapshot, version string
var latest bool

ScaffoldCmd.Flags().StringVarP(&name, "name", "n",
"", "Integration flow name")
Expand All @@ -408,6 +435,8 @@ func init() {
false, "Use underscore as a file splitter; default is __")
ScaffoldCmd.Flags().BoolVarP(&extractCode, "extract-code", "x",
false, "Extract JavaScript and Jsonnet code as separate files")
ScaffoldCmd.Flags().BoolVarP(&latest, "latest", "",
true, "Scaffolds the integeration version in ACTIVE state, if not found the highest snapshot in SNAPSHOT state; default is true")

_ = ScaffoldCmd.MarkFlagRequired("name")
}
Expand Down

0 comments on commit c709632

Please sign in to comment.