Skip to content

Commit

Permalink
Refactored pipeline commands to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
bhanurp committed Jan 26, 2023
1 parent b7f2751 commit 69008f9
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 166 deletions.
2 changes: 1 addition & 1 deletion docs/pipelines/status/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package status
var Usage = []string{"pl status"}

func GetDescription() string {
return "Fetch latest pipeline run status."
return "Fetch the latest pipeline run status."
}
2 changes: 1 addition & 1 deletion docs/pipelines/sync/help.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sync

var Usage = []string{"pl sync <repository name>"}
var Usage = []string{"pl sync"}

func GetDescription() string {
return "Sync a pipeline resource."
Expand Down
2 changes: 1 addition & 1 deletion docs/pipelines/syncstatus/help.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package syncstatus

var Usage = []string{"pl syncstatus"}
var Usage = []string{"pl sync status"}

func GetDescription() string {
return "Fetch pipeline resource sync status."
Expand Down
4 changes: 2 additions & 2 deletions docs/pipelines/trigger/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func GetDescription() string {

func GetArguments() string {
return ` pipeline name
Pipeline name to trigger manual run.
Pipeline name to trigger the manual run.
branch name
Branch name to trigger manual run.`
Branch name to trigger the manual run.`
}
2 changes: 1 addition & 1 deletion docs/pipelines/version/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package version
var Usage = []string{"pl version"}

func GetDescription() string {
return "Get pipelines version."
return "Show the version of JFrog Pipelines."
}
126 changes: 124 additions & 2 deletions pipelines/cli.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package pipelines

import (
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
corecommon "github.com/jfrog/jfrog-cli-core/v2/docs/common"
pipelines "github.com/jfrog/jfrog-cli-core/v2/pipelines/commands"
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli/docs/common"
"github.com/jfrog/jfrog-cli/docs/pipelines/status"
"github.com/jfrog/jfrog-cli/docs/pipelines/sync"
"github.com/jfrog/jfrog-cli/docs/pipelines/syncstatus"
"github.com/jfrog/jfrog-cli/docs/pipelines/trigger"
"github.com/jfrog/jfrog-cli/docs/pipelines/version"

"github.com/jfrog/jfrog-cli/utils/cliutils"
clientlog "github.com/jfrog/jfrog-client-go/utils/log"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -64,11 +71,11 @@ func GetCommands() []cli.Command {
},
},
{
Name: "syncstatus",
Name: "sync-status",
Flags: cliutils.GetCommandFlags(cliutils.SyncStatus),
Aliases: []string{"ss"},
Usage: syncstatus.GetDescription(),
HelpName: corecommon.CreateUsage("pl syncstatus", syncstatus.GetDescription(), syncstatus.Usage),
HelpName: corecommon.CreateUsage("pl sync-status", syncstatus.GetDescription(), syncstatus.Usage),
ArgsUsage: common.CreateEnvVars(),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: func(c *cli.Context) error {
Expand All @@ -77,3 +84,118 @@ func GetCommands() []cli.Command {
},
})
}

// getMultiBranch parses singleBranch flag and computes whether multiBranch is set to true/false
func getMultiBranch(c *cli.Context) bool {
return !c.Bool("single-branch")
}

// createPipelinesDetailsByFlags creates pipelines configuration details
func createPipelinesDetailsByFlags(c *cli.Context) (*coreConfig.ServerDetails, error) {
plDetails, err := cliutils.CreateServerDetailsWithConfigOffer(c, true, cliutils.CmdPipelines)
if err != nil {
return nil, err
}
if plDetails.DistributionUrl == "" {
return nil, fmt.Errorf("the --pipelines-url option is mandatory")
}
return plDetails, nil
}

// fetchLatestPipelineRunStatus fetches pipeline run status and filters from pipeline-name and branch flags
func fetchLatestPipelineRunStatus(c *cli.Context) error {
clientlog.Info(coreutils.PrintTitle("Fetching pipeline run status"))

// Read flags for status command
pipName := c.String("pipeline-name")
notify := c.Bool("monitor")
branch := c.String("branch")
multiBranch := getMultiBranch(c)
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}
statusCommand := pipelines.NewStatusCommand()
statusCommand.SetBranch(branch).
SetPipeline(pipName).
SetNotify(notify).
SetMultiBranch(multiBranch)

// Set server details
statusCommand.SetServerDetails(serviceDetails)
return commands.Exec(statusCommand)
}

// syncPipelineResources sync pipelines resource
func syncPipelineResources(c *cli.Context) error {
// Get arguments repository name and branch name
repository := c.Args().Get(0)
branch := c.Args().Get(1)
clientlog.Info("Triggering pipeline sync on repository:", repository, "branch:", branch)
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}

// Create new sync command and add filters
syncCommand := pipelines.NewSyncCommand()
syncCommand.SetBranch(branch)
syncCommand.SetRepositoryFullName(repository)
syncCommand.SetServerDetails(serviceDetails)
return commands.Exec(syncCommand)
}

// getSyncPipelineResourcesStatus fetch sync status for a given repository path and branch name
func getSyncPipelineResourcesStatus(c *cli.Context) error {
branch := c.String("branch")
repository := c.String("repository")
clientlog.Info("Fetching pipeline sync status on repository:", repository, "branch:", branch)

// Fetch service details for authentication
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}

// Create sync status command and add filter params
syncStatusCommand := pipelines.NewSyncStatusCommand()
syncStatusCommand.SetBranch(branch)
syncStatusCommand.SetRepoPath(repository)
syncStatusCommand.SetServerDetails(serviceDetails)
return commands.Exec(syncStatusCommand)
}

// getVersion version command handler
func getVersion(c *cli.Context) error {
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}
versionCommand := pipelines.NewVersionCommand()
versionCommand.SetServerDetails(serviceDetails)
return commands.Exec(versionCommand)
}

// triggerNewRun triggers a new run for supplied flag values
func triggerNewRun(c *cli.Context) error {
// Read arguments pipeline name and branch to trigger pipeline run
pipelineName := c.Args().Get(0)
branch := c.Args().Get(1)
multiBranch := getMultiBranch(c)
coreutils.PrintTitle("Triggering pipeline run ")
clientlog.Info("Triggering on pipeline:", pipelineName, "for branch:", branch)

// Get service config details
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}

// Trigger a pipeline run using branch name and pipeline name
triggerCommand := pipelines.NewTriggerCommand()
triggerCommand.SetBranch(branch).
SetPipelineName(pipelineName).
SetServerDetails(serviceDetails).
SetMultiBranch(multiBranch)
return commands.Exec(triggerCommand)
}
25 changes: 0 additions & 25 deletions pipelines/config.go

This file was deleted.

33 changes: 0 additions & 33 deletions pipelines/runstatus.go

This file was deleted.

47 changes: 0 additions & 47 deletions pipelines/sync.go

This file was deleted.

18 changes: 0 additions & 18 deletions pipelines/systeminfo.go

This file was deleted.

33 changes: 0 additions & 33 deletions pipelines/triggerrun.go

This file was deleted.

4 changes: 2 additions & 2 deletions utils/cliutils/commandsflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ const (
repository = "repository"
singleBranch = "single-branch"
Sync = "sync"
SyncStatus = "syncstatus"
SyncStatus = "sync-status"

// *** TransferInstall Commands' flags ***
installPluginPrefix = "install-"
Expand Down Expand Up @@ -1480,7 +1480,7 @@ var flagsMap = map[string]cli.Flag{
singleBranch: cli.BoolFlag{
Name: singleBranch,
Usage: "[Default: false] Single branch to filter multi branches and single branch pipelines sources.` `",
},
},
Stop: cli.BoolFlag{
Name: Stop,
Usage: "[Default: false] Set to true to stop the transfer-files command currently in progress. Useful when running the transfer-files command in the background.` `",
Expand Down

0 comments on commit 69008f9

Please sign in to comment.