Skip to content

Commit

Permalink
dagger: function completion
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Feb 16, 2024
1 parent 0829bdc commit bd4df3a
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 22 deletions.
4 changes: 2 additions & 2 deletions completers/code_completer/cmd/action/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/cache/key"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -87,5 +87,5 @@ func ActionMicrosoftExtensions(category string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
// Microsoft extensions are aliased and only found by the 'Microsoft' publisher (though the actual name differs)
return ActionExtensionSearch(category).Invoke(carapace.Context{Value: "Microsoft"}).ToA()
}).Cache(24*time.Hour, cache.String(category))
}).Cache(24*time.Hour, key.String(category))
}
3 changes: 2 additions & 1 deletion completers/dagger_completer/cmd/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var callCmd = &cobra.Command{

func init() {
carapace.Gen(callCmd).Standalone()
callCmd.Flags().SetInterspersed(false)

callCmd.PersistentFlags().Bool("focus", false, "Only show output for focused commands")
callCmd.PersistentFlags().Bool("json", false, "Present result as JSON")
Expand All @@ -28,6 +29,6 @@ func init() {
})

carapace.Gen(callCmd).PositionalAnyCompletion(
dagger.ActionFunctions(),
dagger.ActionFunctionsA(),
)
}
1 change: 1 addition & 0 deletions completers/dagger_completer/cmd/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func init() {
carapace.Gen(functionsCmd).Standalone()

functionsCmd.PersistentFlags().Bool("focus", false, "Only show output for focused commands")
functionsCmd.PersistentFlags().Bool("json", false, "Output as json")
functionsCmd.PersistentFlags().StringP("mod", "m", "", "Path to dagger.json config file for the module or a directory containing that file. Either local path (e.g. \"/path/to/some/dir\") or a github repo (e.g. \"github.com/dagger/dagger/path/to/some/subdir\")")
rootCmd.AddCommand(functionsCmd)

Expand Down
6 changes: 6 additions & 0 deletions completers/dagger_completer/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bridge/pkg/actions/bridge"
"github.com/spf13/cobra"
)

Expand All @@ -15,8 +16,13 @@ var runCmd = &cobra.Command{

func init() {
carapace.Gen(runCmd).Standalone()
runCmd.Flags().SetInterspersed(false)

runCmd.Flags().String("cleanup-timeout", "", "max duration to wait between SIGTERM and SIGKILL on interrupt")
runCmd.Flags().Bool("focus", false, "Only show output for focused commands.")
rootCmd.AddCommand(runCmd)

carapace.Gen(runCmd).PositionalAnyCompletion(
bridge.ActionCarapaceBin(),
)
}
4 changes: 2 additions & 2 deletions completers/gradle_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/cache/key"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/pkg/util"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -126,7 +126,7 @@ func ActionTasks() carapace.Action {
if buildConfig, err := locateBuildConfig(); err != nil {
return "", err
} else {
return cache.FileChecksum(buildConfig)()
return key.FileChecksum(buildConfig)()
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions completers/pulumi_completer/cmd/action/urn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/cache/key"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -47,7 +47,7 @@ func ActionUrns(cmd *cobra.Command) carapace.Action {
return "", err
}

return cache.String(absWd, stack)()
return key.String(absWd, stack)()
},
).Invoke(c).ToA()
})
Expand Down
4 changes: 2 additions & 2 deletions completers/sdkmanager_completer/cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/cache/key"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -36,7 +36,7 @@ func ActionAvailablePackages(cmd *cobra.Command) carapace.Action {
}
}
return carapace.ActionValuesDescribed(vals...)
}).Cache(1*time.Hour, cache.String(sdk_root, channel))
}).Cache(1*time.Hour, key.String(sdk_root, channel))
})
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ require (
)

replace github.com/spf13/pflag => github.com/rsteube/carapace-pflag v0.2.0

replace github.com/rsteube/carapace => ../carapace/
4 changes: 2 additions & 2 deletions pkg/actions/fs/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/cache/key"
"github.com/rsteube/carapace/pkg/style"
)

Expand All @@ -18,6 +18,6 @@ func ActionTarFileContents(file string) carapace.Action {
return carapace.ActionExecCommand("tar", "--list", "--file", file)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines[:len(lines)-1]...)
}).Cache(24*time.Hour, cache.FileStats(file)).Invoke(c).ToMultiPartsA("/").StyleF(style.ForPathExt)
}).Cache(24*time.Hour, key.FileStats(file)).Invoke(c).ToMultiPartsA("/").StyleF(style.ForPathExt)
})
}
107 changes: 107 additions & 0 deletions pkg/actions/tools/dagger/function.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package dagger

import (
"encoding/json"
"os"
"regexp"
"strings"
"time"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/cache/key"
"github.com/spf13/cobra"
)

// ActionFunctions completes functions
Expand All @@ -25,3 +31,104 @@ func ActionFunctions() carapace.Action {
return carapace.ActionValuesDescribed(vals...)
})
}

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
}

func ActionFunctionsA() 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 {
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 {
// 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()
default:
return carapace.ActionValues()
}
}),
})
}

cmd.AddCommand(subCmd)
}

return carapace.ActionExecute(cmd)
})
}
8 changes: 4 additions & 4 deletions pkg/actions/tools/dbt/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/cache/key"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/pkg/util"
)
Expand Down Expand Up @@ -36,8 +36,8 @@ func (o ResourceOpts) Default() ResourceOpts {
return o
}

func (o ResourceOpts) key() cache.Key {
return cache.String(
func (o ResourceOpts) key() key.Key {
return key.String(
strconv.FormatBool(o.Analysis),
strconv.FormatBool(o.Exposure),
strconv.FormatBool(o.Metric),
Expand Down Expand Up @@ -118,6 +118,6 @@ func ActionResources(opts ResourceOpts) carapace.Action {
}
}
return carapace.ActionStyledValuesDescribed(vals...)
}).Cache(24*time.Hour, opts.key(), cache.FileStats(path)) // TODO opts , manifest.json constantly gets new modification time??
}).Cache(24*time.Hour, opts.key(), key.FileStats(path)) // TODO opts , manifest.json constantly gets new modification time??
}).Tag("resources")
}
6 changes: 4 additions & 2 deletions pkg/actions/tools/gh/gh.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// package gh contains github related actions
package gh

import "github.com/rsteube/carapace/pkg/cache"
import (
"github.com/rsteube/carapace/pkg/cache/key"
)

type HostOpts struct {
Host string
Expand All @@ -27,4 +29,4 @@ type RepoOpts struct {
Name string
}

func (o RepoOpts) cacheKey() cache.Key { return cache.String(o.Host, o.Owner, o.Name) }
func (o RepoOpts) cacheKey() key.Key { return key.String(o.Host, o.Owner, o.Name) }
4 changes: 2 additions & 2 deletions pkg/actions/tools/mvn/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/cache/key"
"github.com/rsteube/carapace/pkg/util"
)

Expand All @@ -26,6 +26,6 @@ func ActionProjects(file string) carapace.Action {
return carapace.ActionExecCommand("mvn", args...)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines[:len(lines)-1]...)
}).Cache(-1, cache.FileStats(file))
}).Cache(-1, key.FileStats(file))
})
}
6 changes: 3 additions & 3 deletions pkg/actions/tools/youtubedl/youtubedl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/rsteube/carapace/pkg/cache/key"
)

// ActionFormats completes formats
Expand Down Expand Up @@ -35,7 +35,7 @@ func ActionFormats(url string) carapace.Action {
}
}
return carapace.ActionValuesDescribed(vals...)
}).Cache(1*time.Hour, cache.String(c.Args[0]))
}).Cache(1*time.Hour, key.String(c.Args[0]))
})
}

Expand Down Expand Up @@ -64,6 +64,6 @@ func ActionSubLangs(url string) carapace.Action {
}
}
return carapace.ActionValuesDescribed(vals...)
}).Cache(1*time.Hour, cache.String(c.Args[0]))
}).Cache(1*time.Hour, key.String(c.Args[0]))
})
}

0 comments on commit bd4df3a

Please sign in to comment.