Skip to content

Commit

Permalink
feat: add support for uploading files to tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed May 19, 2023
1 parent dbd7b87 commit 7a1b87f
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 30 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e
rootCmd.AddCommand(importCmd)
rootCmd.AddCommand(exportCmd)
rootCmd.AddCommand(whoamiCmd)
rootCmd.AddCommand(uploadCmd)
}

// version/build information command
Expand Down
70 changes: 68 additions & 2 deletions cmd/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
"github.com/uselagoon/lagoon-cli/internal/lagoon/client"
"github.com/uselagoon/lagoon-cli/pkg/api"
"github.com/uselagoon/lagoon-cli/pkg/output"
"io/ioutil"
"os"

l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
)

var getTaskByID = &cobra.Command{
Expand Down Expand Up @@ -276,6 +281,65 @@ Path:
},
}

var uploadFilesToTask = &cobra.Command{
Use: "task-files",
Short: "Upload files to a task by its ID",
Long: `Upload files to a task by its ID`,
Aliases: []string{"tf"},
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}

taskID, err := cmd.Flags().GetInt("id")
if err != nil {
return err
}
if taskID == 0 {
return fmt.Errorf("Missing arguments: ID is not defined")
}
files, err := cmd.Flags().GetStringSlice("file")
if err != nil {
return err
}
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)
result, err := l.UploadFilesForTask(context.TODO(), taskID, files, lc)
if err != nil {
return err
}
taskFiles := []string{}
for _, f := range result.Files {
taskFiles = append(taskFiles, f.Filename)
}
dataMain := output.Table{
Header: []string{
"ID",
"Name",
"Files",
},
Data: []output.Data{
{
fmt.Sprintf("%d", result.ID),
returnNonEmptyString(result.Name),
returnNonEmptyString(strings.Join(taskFiles, ",")),
},
},
}
output.RenderOutput(dataMain, outputOptions)
return nil
},
}

var (
taskName string
invokedTaskName string
Expand All @@ -285,6 +349,8 @@ var (
)

func init() {
uploadFilesToTask.Flags().IntP("id", "I", 0, "ID of the task")
uploadFilesToTask.Flags().StringSliceP("file", "F", []string{}, "File to upload (add multiple flags to upload multiple files)")
invokeDefinedTask.Flags().StringVarP(&invokedTaskName, "name", "N", "", "Name of the task that will be invoked")
runCustomTask.Flags().StringVarP(&taskName, "name", "N", "Custom Task", "Name of the task that will show in the UI (default: Custom Task)")
runCustomTask.Flags().StringVarP(&taskService, "service", "S", "cli", "Name of the service (cli, nginx, other) that should run the task (default: cli)")
Expand Down
18 changes: 18 additions & 0 deletions cmd/upload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/spf13/cobra"
)

var uploadCmd = &cobra.Command{
Use: "upload",
Aliases: []string{"u"},
Short: "Upload files to tasks",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
validateToken(lagoonCLIConfig.Current) // get a new token if the current one is invalid
},
}

func init() {
uploadCmd.AddCommand(uploadFilesToTask)
}
1 change: 1 addition & 0 deletions docs/commands/lagoon.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ lagoon [flags]
* [lagoon run](lagoon_run.md) - Run a task against an environment
* [lagoon ssh](lagoon_ssh.md) - Display the SSH command to access a specific environment in a project
* [lagoon update](lagoon_update.md) - Update a resource
* [lagoon upload](lagoon_upload.md) - Upload files to tasks
* [lagoon version](lagoon_version.md) - Version information
* [lagoon web](lagoon_web.md) - Launch the web user interface
* [lagoon whoami](lagoon_whoami.md) - Whoami will return your user information for lagoon
Expand Down
36 changes: 36 additions & 0 deletions docs/commands/lagoon_upload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## lagoon upload

Upload files to tasks

### Synopsis

Upload files to tasks

### Options

```
-h, --help help for upload
```

### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
-l, --lagoon string The Lagoon instance to interact with
--no-header No header on table (if supported)
--output-csv Output as CSV (if supported)
--output-json Output as JSON (if supported)
--pretty Make JSON pretty (if supported)
-p, --project string Specify a project to use
--skip-update-check Skip checking for updates
-i, --ssh-key string Specify path to a specific SSH key to use for lagoon authentication
```

### SEE ALSO

* [lagoon](lagoon.md) - Command line integration for Lagoon
* [lagoon upload task-files](lagoon_upload_task-files.md) - Upload files to a task by its ID

41 changes: 41 additions & 0 deletions docs/commands/lagoon_upload_task-files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## lagoon upload task-files

Upload files to a task by its ID

### Synopsis

Upload files to a task by its ID

```
lagoon upload task-files [flags]
```

### Options

```
-F, --file strings File to upload (add multiple flags to upload multiple files)
-h, --help help for task-files
-I, --id int ID of the task
```

### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
-l, --lagoon string The Lagoon instance to interact with
--no-header No header on table (if supported)
--output-csv Output as CSV (if supported)
--output-json Output as JSON (if supported)
--pretty Make JSON pretty (if supported)
-p, --project string Specify a project to use
--skip-update-check Skip checking for updates
-i, --ssh-key string Specify path to a specific SSH key to use for lagoon authentication
```

### SEE ALSO

* [lagoon upload](lagoon_upload.md) - Upload files to tasks

15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/golang/mock v1.6.0
github.com/google/go-github v0.0.0-20180716180158-c0b63e2f9bb1
github.com/google/uuid v1.1.1
github.com/hashicorp/go-version v1.2.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-version v1.6.0
github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc
github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434
github.com/machinebox/graphql v0.2.3-0.20181106130121-3a9253180225
Expand All @@ -17,8 +17,8 @@ require (
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.2.2
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
github.com/stretchr/testify v1.8.2
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b
gopkg.in/yaml.v2 v2.2.8
sigs.k8s.io/yaml v1.2.0
)
Expand All @@ -27,14 +27,12 @@ require (
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/guregu/null v4.0.0+incompatible
github.com/matryer/is v1.2.0 // indirect
// workaround for https://github.com/manifoldco/promptui/issues/98
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/uselagoon/machinery v0.0.8
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect
rsc.io/quote/v3 v3.1.0 // indirect
)

// use this version for fixes to formatting of end header
Expand All @@ -43,3 +41,6 @@ replace github.com/olekukonko/tablewriter => github.com/shreddedbacon/tablewrite
// replace github.com/machinebox/graphql => ../../shreddedbacon/graphql

// replace github.com/olekukonko/tablewriter => ../../shreddedbacon/tablewriter

// replace github.com/uselagoon/machinery v0.0.8 => ../machinery
replace github.com/uselagoon/machinery v0.0.8 => github.com/uselagoon/machinery v0.0.0-20230518215531-41f1b4bb9b26
Loading

0 comments on commit 7a1b87f

Please sign in to comment.