Skip to content

Commit

Permalink
package flag
Browse files Browse the repository at this point in the history
  • Loading branch information
daveads authored and ezekg committed Oct 4, 2024
1 parent 3f60c63 commit eee45ca
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 28 deletions.
34 changes: 28 additions & 6 deletions cmd/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ var (
Docs:
https://keygen.sh/docs/cli/`,
Args: cobra.NoArgs,
RunE: delRun,

// Encountering an error should not display usage
Args: cobra.NoArgs,
RunE: delRun,
SilenceUsage: true,
}
)

type DeleteCommandOptions struct {
Release string
Package string
Artifact string
NoAutoUpgrade bool
}
Expand All @@ -44,6 +43,7 @@ func init() {
delCmd.Flags().StringVar(&keygenext.Environment, "environment", "", "your keygen.sh environment identifier [$KEYGEN_ENVIRONMENT=<id>]")
delCmd.Flags().StringVar(&keygenext.APIURL, "host", "", "the host of the keygen server [$KEYGEN_HOST=<host>]")
delCmd.Flags().StringVar(&delOpts.Release, "release", "", "the release identifier (required)")
delCmd.Flags().StringVar(&delOpts.Package, "package", "", "package identifier for the release")
delCmd.Flags().StringVar(&delOpts.Artifact, "artifact", "", "the artifact identifier")
delCmd.Flags().BoolVar(&delOpts.NoAutoUpgrade, "no-auto-upgrade", false, "disable automatic upgrade checks [$KEYGEN_NO_AUTO_UPGRADE=1]")

Expand Down Expand Up @@ -114,14 +114,36 @@ func delRun(cmd *cobra.Command, args []string) error {

var deletable interface {
jsonapi.MarshalResourceIdentifier

Delete() error
}

switch {
case delOpts.Artifact != "":
deletable = &keygenext.Artifact{ReleaseID: &delOpts.Release, ID: delOpts.Artifact}
default:
deletable = &keygenext.Release{ID: delOpts.Release}
release := &keygenext.Release{
ID: delOpts.Release,
PackageID: &delOpts.Package,
}

if err := release.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
if e.Code != "" {
code = italic("(" + e.Code + ")")
}

if e.Source != "" {
return fmt.Errorf("%s: %s %s %s", e.Title, e.Source, e.Detail, code)
} else {
return fmt.Errorf("%s: %s %s", e.Title, e.Detail, code)
}
}

return err
}

deletable = release
}

if err := deletable.Delete(); err != nil {
Expand Down
28 changes: 23 additions & 5 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ var (
Docs:
https://keygen.sh/docs/cli/`,
Args: cobra.NoArgs,
RunE: publishRun,

// Encountering an error should not display usage
Args: cobra.NoArgs,
RunE: publishRun,
SilenceUsage: true,
}
)

type PublishCommandOptions struct {
Release string
Package string
NoAutoUpgrade bool
}

Expand All @@ -41,6 +40,7 @@ func init() {
publishCmd.Flags().StringVar(&keygenext.Environment, "environment", "", "your keygen.sh environment identifier [$KEYGEN_ENVIRONMENT=<id>]")
publishCmd.Flags().StringVar(&keygenext.APIURL, "host", "", "the host of the keygen server [$KEYGEN_HOST=<host>]")
publishCmd.Flags().StringVar(&publishOpts.Release, "release", "", "the release identifier (required)")
publishCmd.Flags().StringVar(&publishOpts.Package, "package", "", "package identifier for the release")
publishCmd.Flags().BoolVar(&publishOpts.NoAutoUpgrade, "no-auto-upgrade", false, "disable automatic upgrade checks [$KEYGEN_NO_AUTO_UPGRADE=1]")

if v, ok := os.LookupEnv("KEYGEN_ACCOUNT_ID"); ok {
Expand Down Expand Up @@ -109,7 +109,25 @@ func publishRun(cmd *cobra.Command, args []string) error {
}

release := &keygenext.Release{
ID: publishOpts.Release,
ID: publishOpts.Release,
PackageID: &publishOpts.Package,
}

if err := release.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
if e.Code != "" {
code = italic("(" + e.Code + ")")
}

if e.Source != "" {
return fmt.Errorf("%s: %s %s %s", e.Title, e.Source, e.Detail, code)
} else {
return fmt.Errorf("%s: %s %s", e.Title, e.Detail, code)
}
}

return err
}

if err := release.Publish(); err != nil {
Expand Down
31 changes: 25 additions & 6 deletions cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ var (
Docs:
https://keygen.sh/docs/cli/`,
Args: tagArgs,
RunE: tagRun,

// Encountering an error should not display usage
Args: tagArgs,
RunE: tagRun,
SilenceUsage: true,
}
)

type TagCommandOptions struct {
Release string
Package string
NoAutoUpgrade bool
}

Expand All @@ -42,6 +41,7 @@ func init() {
tagCmd.Flags().StringVar(&keygenext.Environment, "environment", "", "your keygen.sh environment identifier [$KEYGEN_ENVIRONMENT=<id>]")
tagCmd.Flags().StringVar(&keygenext.APIURL, "host", "", "the host of the keygen server [$KEYGEN_HOST=<host>]")
tagCmd.Flags().StringVar(&tagOpts.Release, "release", "", "the release identifier (required)")
tagCmd.Flags().StringVar(&tagOpts.Package, "package", "", "package identifier for the release")
tagCmd.Flags().BoolVar(&tagOpts.NoAutoUpgrade, "no-auto-upgrade", false, "disable automatic upgrade checks [$KEYGEN_NO_AUTO_UPGRADE=1]")

if v, ok := os.LookupEnv("KEYGEN_ACCOUNT_ID"); ok {
Expand Down Expand Up @@ -118,10 +118,29 @@ func tagRun(cmd *cobra.Command, args []string) error {
}

release := &keygenext.Release{
ID: tagOpts.Release,
Tag: &args[0],
ID: tagOpts.Release,
PackageID: &tagOpts.Package,
}

if err := release.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
if e.Code != "" {
code = italic("(" + e.Code + ")")
}

if e.Source != "" {
return fmt.Errorf("%s: %s %s %s", e.Title, e.Source, e.Detail, code)
} else {
return fmt.Errorf("%s: %s %s", e.Title, e.Detail, code)
}
}

return err
}

release.Tag = &args[0]

if err := release.Update(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
Expand Down
31 changes: 25 additions & 6 deletions cmd/untag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ var (
Docs:
https://keygen.sh/docs/cli/`,
Args: cobra.NoArgs,
RunE: untagRun,

// Encountering an error should not display usage
Args: cobra.NoArgs,
RunE: untagRun,
SilenceUsage: true,
}
)

type UntagCommandOptions struct {
Release string
Package string
NoAutoUpgrade bool
}

Expand All @@ -41,6 +40,7 @@ func init() {
untagCmd.Flags().StringVar(&keygenext.Environment, "environment", "", "your keygen.sh environment identifier [$KEYGEN_ENVIRONMENT=<id>]")
untagCmd.Flags().StringVar(&keygenext.APIURL, "host", "", "the host of the keygen server [$KEYGEN_HOST=<host>]")
untagCmd.Flags().StringVar(&untagOpts.Release, "release", "", "the release identifier (required)")
untagCmd.Flags().StringVar(&untagOpts.Package, "package", "", "package identifier for the release")
untagCmd.Flags().BoolVar(&untagOpts.NoAutoUpgrade, "no-auto-upgrade", false, "disable automatic upgrade checks [$KEYGEN_NO_AUTO_UPGRADE=1]")

if v, ok := os.LookupEnv("KEYGEN_ACCOUNT_ID"); ok {
Expand Down Expand Up @@ -109,10 +109,29 @@ func untagRun(cmd *cobra.Command, args []string) error {
}

release := &keygenext.Release{
ID: untagOpts.Release,
Tag: nil,
ID: untagOpts.Release,
PackageID: &untagOpts.Package,
}

if err := release.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
if e.Code != "" {
code = italic("(" + e.Code + ")")
}

if e.Source != "" {
return fmt.Errorf("%s: %s %s %s", e.Title, e.Source, e.Detail, code)
} else {
return fmt.Errorf("%s: %s %s", e.Title, e.Detail, code)
}
}

return err
}

release.Tag = nil

if err := release.Update(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
Expand Down
28 changes: 23 additions & 5 deletions cmd/yank.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ var (
Docs:
https://keygen.sh/docs/cli/`,
Args: cobra.NoArgs,
RunE: yankRun,

// Encountering an error should not display usage
Args: cobra.NoArgs,
RunE: yankRun,
SilenceUsage: true,
}
)

type YankCommandOptions struct {
Release string
Package string
NoAutoUpgrade bool
}

Expand All @@ -41,6 +40,7 @@ func init() {
yankCmd.Flags().StringVar(&keygenext.Environment, "environment", "", "your keygen.sh environment identifier [$KEYGEN_ENVIRONMENT=<id>]")
yankCmd.Flags().StringVar(&keygenext.APIURL, "host", "", "the host of the keygen server [$KEYGEN_HOST=<host>]")
yankCmd.Flags().StringVar(&yankOpts.Release, "release", "", "the release identifier (required)")
yankCmd.Flags().StringVar(&yankOpts.Package, "package", "", "package identifier for the release")
yankCmd.Flags().BoolVar(&yankOpts.NoAutoUpgrade, "no-auto-upgrade", false, "disable automatic upgrade checks [$KEYGEN_NO_AUTO_UPGRADE=1]")

if v, ok := os.LookupEnv("KEYGEN_ACCOUNT_ID"); ok {
Expand Down Expand Up @@ -109,7 +109,25 @@ func yankRun(cmd *cobra.Command, args []string) error {
}

release := &keygenext.Release{
ID: yankOpts.Release,
ID: yankOpts.Release,
PackageID: &yankOpts.Package,
}

if err := release.Get(); err != nil {
if e, ok := err.(*keygenext.Error); ok {
var code string
if e.Code != "" {
code = italic("(" + e.Code + ")")
}

if e.Source != "" {
return fmt.Errorf("%s: %s %s %s", e.Title, e.Source, e.Detail, code)
} else {
return fmt.Errorf("%s: %s %s", e.Title, e.Detail, code)
}
}

return err
}

if err := release.Yank(); err != nil {
Expand Down

0 comments on commit eee45ca

Please sign in to comment.