Skip to content

Commit

Permalink
root: move jfrog env vars check and validation to PreRunE
Browse files Browse the repository at this point in the history
  • Loading branch information
albertocavalcante committed Aug 22, 2024
1 parent 387165e commit 5d8b8f5
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,38 @@ func (f *GarfFlags) addFlags(cmd *cobra.Command) {
func NewRootCmd() *cobra.Command {
f := &GarfFlags{}

var jfConfig *core.JFrogConfig

cmd := &cobra.Command{
Use: "garf",
Short: "garf is a CLI to mirror artifacts",
Long: `A CLI to mirror artifacts from places such as
GitHub Releases to registries such as
JFrog Artifactory.`,
RunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
if f.Source == "" || f.Destination == "" {
return fmt.Errorf("--source and --destination flags are required")
}

// Get required environment variables
jfrogUrl := os.Getenv("JFROG_URL")
jfrogUser := os.Getenv("JFROG_USER")
jfrogPassword := os.Getenv("JFROG_PASSWORD")

// Validate required environment variables
if jfrogUrl == "" || jfrogUser == "" || jfrogPassword == "" {
return fmt.Errorf("JFROG_URL, JFROG_USER and JFROG_PASSWORD environment variables are required")
}

jfConfig = &core.JFrogConfig{
Url: jfrogUrl,
User: jfrogUser,
Password: jfrogPassword,
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
artifactCoordinates, err := artifact.ExtractCoordinatesFromURL(f.Source)
if err != nil {
return err
Expand All @@ -63,7 +84,12 @@ func NewRootCmd() *cobra.Command {
fmt.Printf("Artifact downloaded to %s\n", location)
}

err = core.UploadGenericArtifact(location, f.Destination, artifactCoordinates)
jfClient, err := core.NewJFrogClient(jfConfig)
if err != nil {
return err
}

err = jfClient.UploadGenericArtifact(location, f.Destination, artifactCoordinates)
if err != nil {
return err
}
Expand Down

0 comments on commit 5d8b8f5

Please # to comment.