/
variable.go
98 lines (88 loc) · 2.57 KB
/
variable.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
package gh
import (
"fmt"
"time"
"github.com/rsteube/carapace"
)
type variable struct {
Name string `json:"name"`
Value string `json:"value"`
UpdatedAt time.Time `json:"updated_at"`
}
type EnvironmentOpts struct {
Host string
Owner string
Name string
Environment string
}
func (o EnvironmentOpts) repo() RepoOpts {
return RepoOpts{Host: o.Host, Owner: o.Owner}
}
// ActionEnvironmentVariables completes environment variables
//
// first (value1)
// second (value2)
func ActionEnvironmentVariables(opts EnvironmentOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var queryResult struct {
Variables []variable
}
return apiV3Action(opts.repo(), fmt.Sprintf("repos/%v/%v/environments/github-pages/variables?per_page=100", opts.Owner, opts.Name), &queryResult, func() carapace.Action {
vals := make([]string, 0)
for _, v := range queryResult.Variables {
vals = append(vals, v.Name, v.Value) // TODO updated at
}
return carapace.ActionValuesDescribed(vals...)
})
})
}
// ActionRepositoryVariables completes repository variables
//
// first (value1)
// second (value2)
func ActionRepositoryVariables(opts RepoOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var queryResult struct {
Variables []variable
}
return apiV3Action(opts, fmt.Sprintf("repos/%v/%v/actions/variables?per_page=100", opts.Owner, opts.Name), &queryResult, func() carapace.Action {
vals := make([]string, 0)
for _, v := range queryResult.Variables {
vals = append(vals, v.Name, v.Value) // TODO updated at
}
return carapace.ActionValuesDescribed(vals...)
})
})
}
// ActionOrganisationVariables completes organisation variables
//
// first (value1)
// second (value2)
func ActionOrganisationVariables(opts OwnerOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var queryResult struct {
Variables []variable
}
return apiV3Action(opts.repo(), fmt.Sprintf("orgs/%v/actions/variables?per_page=100", opts.Owner), &queryResult, func() carapace.Action {
vals := make([]string, 0)
for _, v := range queryResult.Variables {
vals = append(vals, v.Name, v.Value) // TODO updated at
}
return carapace.ActionValuesDescribed(vals...)
})
})
}
// ActionVariableFields completes variable fields
//
// name
// value
func ActionVariableFields() carapace.Action {
return carapace.ActionValues(
"name",
"value",
"visibility",
"updatedAt",
"numSelectedRepos",
"selectedReposURL",
)
}