-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathfunction.go
134 lines (117 loc) · 3.4 KB
/
function.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
package dagger
import (
"encoding/json"
"os"
"time"
"unicode"
"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/cache/key"
"github.com/spf13/cobra"
)
type function struct {
Name string `json:"Name"`
Description string `json:"Description"`
Args []struct {
Name string `json:"Name"`
Description string `json:"Description"`
TypeDef struct {
Kind string `json:"Kind"`
Optional bool `json:"Optional"`
AsObject *struct {
Name string `json:"Name"`
Functions any `json:"Functions"`
Fields any `json:"Fields"`
Constructor any `json:"Constructor"`
SourceModuleName string `json:"SourceModuleName"`
} `json:"AsObject"`
AsInterface any `json:"AsInterface"`
AsInput any `json:"AsInput"`
AsList any `json:"AsList"`
} `json:"TypeDef"`
DefaultValue string `json:"DefaultValue"`
} `json:"Args"`
}
type daggerFile struct {
Name string
Sdk string
Source string
}
// ActionFunctions completes functions and their arguments
//
// grep-dir (dagger call grep-dir --directory-arg . --pattern GrepDir)
// --directory-arg
func ActionFunctions() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
content, err := os.ReadFile("dagger.json")
if err != nil {
return carapace.ActionMessage(err.Error())
}
var d daggerFile
if err := json.Unmarshal(content, &d); err != nil {
return carapace.ActionMessage(err.Error())
}
output, err := cache.Cache(24*time.Hour, key.FolderStats(d.Source))(func() ([]byte, error) {
return c.Command("dagger", "functions", "--json").Output()
})
if err != nil {
return carapace.ActionMessage(err.Error())
}
var functions []function
if err := json.Unmarshal(output, &functions); err != nil {
return carapace.ActionMessage(err.Error())
}
cmd := &cobra.Command{}
carapace.Gen(cmd).Standalone()
for _, f := range functions {
f.Name = toKebab(f.Name)
subCmd := &cobra.Command{
Use: f.Name,
Short: f.Description,
Run: func(cmd *cobra.Command, args []string) {},
}
carapace.Gen(subCmd).Standalone()
for _, arg := range f.Args {
arg.Name = toKebab(arg.Name)
// TODO transform camelcase to kebab
switch arg.TypeDef.Kind { // TODO more types
case "STRING_KIND", "OBJECT_KIND":
subCmd.Flags().String(arg.Name, arg.DefaultValue, arg.Description)
default:
return carapace.ActionMessage("unknown kind %s", arg.TypeDef.Kind)
}
if arg.TypeDef.Optional {
subCmd.Flag(arg.Name).NoOptDefVal = " "
}
localArg := arg
carapace.Gen(subCmd).FlagCompletion(carapace.ActionMap{
arg.Name: carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if localArg.TypeDef.AsObject == nil {
return carapace.ActionValues()
}
switch localArg.TypeDef.AsObject.Name { // TODO more
case "Directory":
return carapace.ActionDirectories()
case "File":
return carapace.ActionFiles()
default:
return carapace.ActionValues()
}
}),
})
}
cmd.AddCommand(subCmd)
}
return carapace.ActionExecute(cmd)
})
}
func toKebab(s string) string {
runes := make([]rune, 0)
for _, r := range []rune(s) {
if unicode.IsUpper(r) {
runes = append(runes, '-')
}
runes = append(runes, unicode.ToLower(r))
}
return string(runes)
}