-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrunsDownload.go
157 lines (127 loc) · 5.04 KB
/
runsDownload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/
package cmd
import (
"log"
"github.com/galasa-dev/cli/pkg/api"
"github.com/galasa-dev/cli/pkg/auth"
"github.com/galasa-dev/cli/pkg/runs"
"github.com/galasa-dev/cli/pkg/utils"
"github.com/spf13/cobra"
)
// Objective: Allow the user to do this:
// runs download --name U123 [--force]
// And then galasactl downloads the artifacts for the given run.
type RunsDownloadCommand struct {
values *RunsDownloadCmdValues
cobraCommand *cobra.Command
}
// Variables set by cobra's command-line parsing.
type RunsDownloadCmdValues struct {
runNameDownload string
runForceDownload bool
runDownloadTargetFolder string
}
// ------------------------------------------------------------------------------------------------
// Constructors methods
// ------------------------------------------------------------------------------------------------
func NewRunsDownloadCommand(factory Factory, runsCommand GalasaCommand, rootCommand GalasaCommand) (GalasaCommand, error) {
cmd := new(RunsDownloadCommand)
err := cmd.init(factory, runsCommand, rootCommand)
return cmd, err
}
// ------------------------------------------------------------------------------------------------
// Public methods
// ------------------------------------------------------------------------------------------------
func (cmd *RunsDownloadCommand) Name() string {
return COMMAND_NAME_RUNS_DOWNLOAD
}
func (cmd *RunsDownloadCommand) CobraCommand() *cobra.Command {
return cmd.cobraCommand
}
func (cmd *RunsDownloadCommand) Values() interface{} {
return cmd.values
}
// ------------------------------------------------------------------------------------------------
// Private methods
// ------------------------------------------------------------------------------------------------
func (cmd *RunsDownloadCommand) init(factory Factory, runsCommand GalasaCommand, rootCommand GalasaCommand) error {
var err error
cmd.values = &RunsDownloadCmdValues{}
cmd.cobraCommand, err = cmd.createRunsDownloadCobraCmd(factory,
runsCommand,
rootCommand.Values().(*RootCmdValues),
)
return err
}
func (cmd *RunsDownloadCommand) createRunsDownloadCobraCmd(
factory Factory,
runsCommand GalasaCommand,
rootCmdValues *RootCmdValues,
) (*cobra.Command, error) {
var err error = nil
runsCmdValues := runsCommand.Values().(*RunsCmdValues)
runsDownloadCobraCmd := &cobra.Command{
Use: "download",
Short: "Download the artifacts of a test run which ran.",
Long: "Download the artifacts of a test run which ran and store them in a directory within the current working directory",
Args: cobra.NoArgs,
Aliases: []string{"runs download"},
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.executeRunsDownload(factory, runsCmdValues, rootCmdValues)
},
}
runsDownloadCobraCmd.PersistentFlags().StringVar(&cmd.values.runNameDownload, "name", "", "the name of the test run we want information about")
runsDownloadCobraCmd.PersistentFlags().BoolVar(&cmd.values.runForceDownload, "force", false, "force artifacts to be overwritten if they already exist")
runsDownloadCobraCmd.MarkPersistentFlagRequired("name")
runsDownloadCobraCmd.PersistentFlags().StringVar(&cmd.values.runDownloadTargetFolder, "destination", ".",
"The folder we want to download test run artifacts into. Sub-folders will be created within this location",
)
runsCommand.CobraCommand().AddCommand(runsDownloadCobraCmd)
return runsDownloadCobraCmd, err
}
func (cmd *RunsDownloadCommand) executeRunsDownload(
factory Factory,
runsCmdValues *RunsCmdValues,
rootCmdValues *RootCmdValues,
) error {
var err error
// Operations on the file system will all be relative to the current folder.
fileSystem := factory.GetFileSystem()
err = utils.CaptureLog(fileSystem, rootCmdValues.logFileName)
if err == nil {
rootCmdValues.isCapturingLogs = true
log.Println("Galasa CLI - Download artifacts for a run")
// Get the ability to query environment variables.
env := factory.GetEnvironment()
var galasaHome utils.GalasaHome
galasaHome, err = utils.NewGalasaHome(fileSystem, env, rootCmdValues.CmdParamGalasaHomePath)
if err == nil {
// Read the bootstrap properties.
var urlService *api.RealUrlResolutionService = new(api.RealUrlResolutionService)
var bootstrapData *api.BootstrapData
bootstrapData, err = api.LoadBootstrap(galasaHome, fileSystem, env, runsCmdValues.bootstrap, urlService)
if err == nil {
var console = factory.GetStdOutConsole()
timeService := factory.GetTimeService()
apiServerUrl := bootstrapData.ApiServerURL
log.Printf("The API server is at '%s'\n", apiServerUrl)
apiClient := auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)
// Call to process the command in a unit-testable way.
err = runs.DownloadArtifacts(
cmd.values.runNameDownload,
cmd.values.runForceDownload,
fileSystem,
timeService,
console,
apiClient,
cmd.values.runDownloadTargetFolder,
)
}
}
}
return err
}