/
datasource_move.go
55 lines (45 loc) · 1.18 KB
/
datasource_move.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
package cmd
import (
"fmt"
"log"
"os"
"github.com/spf13/cobra"
)
var (
datasourceMoveCmd = &cobra.Command{
Use: "mv <organisation-ref> <name> <new-name>",
Aliases: []string{"move", "rename"},
Short: "Rename a fixture",
Args: cobra.ExactArgs(3),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
datasourceOpts.Organisation = lookupOrganisationUID(NewClient(), args[0])
if datasourceOpts.Organisation == "" {
log.Fatal("Missing organisation")
}
},
Run: runDataSourceMove,
ValidArgsFunction: completeOrga,
}
)
func init() {
datasourceCmd.AddCommand(datasourceMoveCmd)
}
func runDataSourceMove(cmd *cobra.Command, args []string) {
client := NewClient()
fileName := args[1]
newFileName := args[2]
fileFixture := findFixtureByName(*client, datasourceOpts.Organisation, fileName)
success, result, err := client.MoveFileFixture(datasourceOpts.Organisation, fileFixture.ID, newFileName)
if err != nil {
log.Fatal(err)
}
if !success {
fmt.Fprintln(os.Stderr, "Could not move data source!")
fmt.Fprintln(os.Stderr, string(result))
os.Exit(1)
}
if rootOpts.OutputFormat == "json" {
fmt.Println(string(result))
return
}
}