-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/rsteube/carapace" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var workCmd = &cobra.Command{ | ||
Use: "work", | ||
Short: "workspace maintenance", | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
func init() { | ||
carapace.Gen(workCmd).Standalone() | ||
|
||
rootCmd.AddCommand(workCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cmd | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/rsteube/carapace" | ||
"github.com/rsteube/carapace-bin/pkg/actions/tools/golang" | ||
"github.com/rsteube/carapace-bin/pkg/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var work_editCmd = &cobra.Command{ | ||
Use: "edit", | ||
Short: "edit go.work from tools or scripts", | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
func init() { | ||
carapace.Gen(work_editCmd).Standalone() | ||
work_editCmd.Flags().StringArrayS("dropreplace", "dropreplace", []string{}, "drop a replacement") | ||
work_editCmd.Flags().StringArrayS("dropuse", "dropuse", []string{}, "drop a use directive") | ||
work_editCmd.Flags().BoolS("fmt", "fmt", false, "reformat the go.work file without making other changes") | ||
work_editCmd.Flags().StringS("go", "go", "", "set the expected Go language version") | ||
work_editCmd.Flags().BoolS("json", "json", false, "print the final go.work in JSON format") | ||
work_editCmd.Flags().BoolS("print", "print", false, "print the final go.work in its text format") | ||
work_editCmd.Flags().StringArrayS("replace", "replace", []string{}, "add a replacement") | ||
work_editCmd.Flags().StringArrayS("use", "use", []string{}, "add a use directive") | ||
|
||
workCmd.AddCommand(work_editCmd) | ||
|
||
carapace.Gen(work_editCmd).FlagCompletion(carapace.ActionMap{ | ||
"dropreplace": carapace.ActionCallback(func(c carapace.Context) carapace.Action { | ||
if len(c.Args) > 0 { | ||
return golang.ActionWorkReplacements(c.Args[0]) | ||
} | ||
return golang.ActionWorkReplacements("") | ||
}), | ||
"dropuse": carapace.ActionCallback(func(c carapace.Context) carapace.Action { | ||
if len(c.Args) > 0 { | ||
return golang.ActionWorkUses(c.Args[0]) | ||
} | ||
return golang.ActionWorkUses("") | ||
}), | ||
"go": golang.ActionVersions(), | ||
"replace": carapace.ActionMultiParts("=", func(c carapace.Context) carapace.Action { | ||
switch len(c.Parts) { | ||
case 0: | ||
if len(c.Args) > 0 { | ||
return golang.ActionWorkModules(c.Args[0]).Invoke(c).Suffix("=").ToA() | ||
} | ||
return golang.ActionWorkModules("").Invoke(c).Suffix("=").ToA() | ||
case 1: | ||
if util.HasPathPrefix(c.CallbackValue) { | ||
path, err := util.FindReverse(c.Dir, "go.work") | ||
if err != nil { | ||
return carapace.ActionMessage(err.Error()) | ||
} | ||
return carapace.ActionFiles().Chdir(filepath.Dir(path)) | ||
} | ||
return golang.ActionModuleSearch() | ||
default: | ||
return carapace.ActionValues() | ||
} | ||
}), | ||
"use": carapace.ActionCallback(func(c carapace.Context) carapace.Action { | ||
path, err := util.FindReverse(c.Dir, "go.work") | ||
if err != nil { | ||
return carapace.ActionMessage(err.Error()) | ||
} | ||
return carapace.ActionFiles().Chdir(filepath.Dir(path)) | ||
|
||
}), | ||
}) | ||
|
||
carapace.Gen(work_editCmd).PositionalCompletion( | ||
carapace.ActionFiles("go.work"), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/rsteube/carapace" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var work_initCmd = &cobra.Command{ | ||
Use: "init", | ||
Short: "initialize workspace file", | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
func init() { | ||
carapace.Gen(work_initCmd).Standalone() | ||
|
||
workCmd.AddCommand(work_initCmd) | ||
|
||
carapace.Gen(work_initCmd).PositionalAnyCompletion( | ||
carapace.ActionDirectories(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/rsteube/carapace" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var work_syncCmd = &cobra.Command{ | ||
Use: "sync", | ||
Short: "sync workspace build list to modules", | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
func init() { | ||
carapace.Gen(work_syncCmd).Standalone() | ||
|
||
workCmd.AddCommand(work_syncCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/rsteube/carapace" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var work_useCmd = &cobra.Command{ | ||
Use: "use", | ||
Short: "add modules to workspace file", | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
func init() { | ||
carapace.Gen(work_useCmd).Standalone() | ||
work_useCmd.Flags().BoolS("r", "r", false, "recursively for modules in the argument directories") | ||
|
||
workCmd.AddCommand(work_useCmd) | ||
|
||
carapace.Gen(work_useCmd).PositionalAnyCompletion( | ||
carapace.ActionDirectories(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package golang | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/rsteube/carapace" | ||
"github.com/rsteube/carapace-bin/pkg/util" | ||
) | ||
|
||
type work struct { | ||
Go string | ||
Use []struct { | ||
DiskPath string | ||
} | ||
Replace []struct { | ||
Old struct { | ||
Path string | ||
} | ||
New struct { | ||
Path string | ||
Version string | ||
} | ||
} | ||
} | ||
|
||
func actionWork(path string, f func(w work) carapace.Action) carapace.Action { | ||
return carapace.ActionCallback(func(c carapace.Context) carapace.Action { | ||
args := []string{"work", "edit", "-json"} | ||
if path != "" { | ||
args = append(args, path) | ||
} | ||
return carapace.ActionExecCommand("go", args...)(func(output []byte) carapace.Action { | ||
var w work | ||
if err := json.Unmarshal(output, &w); err != nil { | ||
return carapace.ActionMessage(err.Error()) | ||
} | ||
return f(w) | ||
}) | ||
}) | ||
} | ||
|
||
// ActionWorkUses completes workspace uses | ||
// | ||
// ./carapace | ||
// ./carapace-bin | ||
func ActionWorkUses(path string) carapace.Action { | ||
return actionWork(path, func(w work) carapace.Action { | ||
vals := make([]string, 0) | ||
for _, use := range w.Use { | ||
vals = append(vals, use.DiskPath) | ||
} | ||
return carapace.ActionValues(vals...) | ||
}) | ||
} | ||
|
||
// ActionWorkReplacements completes workspace replacements | ||
// github.com/rsteube/carapace-spec (github.com/rsteube/carapace-spec@v0.3.0 | ||
// github.com/spf13/pflag (../carapace-pflag/) | ||
func ActionWorkReplacements(path string) carapace.Action { | ||
return actionWork(path, func(w work) carapace.Action { | ||
vals := make([]string, 0) | ||
for _, replace := range w.Replace { | ||
if replace.New.Version != "" { | ||
vals = append(vals, replace.Old.Path, fmt.Sprintf("%v@%v", replace.New.Path, replace.New.Version)) | ||
} else { | ||
vals = append(vals, replace.Old.Path, replace.New.Path) | ||
} | ||
} | ||
return carapace.ActionValuesDescribed(vals...) | ||
}) | ||
} | ||
|
||
// ActionWorkModules completes workspace modules | ||
// | ||
// github.com/pelletier/go-toml | ||
// github.com/rsteube/carapace | ||
func ActionWorkModules(path string) carapace.Action { | ||
return carapace.ActionCallback(func(c carapace.Context) carapace.Action { | ||
return actionWork(path, func(w work) carapace.Action { | ||
if path == "" { | ||
var err error | ||
if path, err = util.FindReverse(path, "go.work"); err != nil { | ||
return carapace.ActionMessage(err.Error()) | ||
} | ||
} | ||
abs, err := c.Abs(path) | ||
if err != nil { | ||
return carapace.ActionMessage(err.Error()) | ||
} | ||
|
||
batch := carapace.Batch() | ||
for _, use := range w.Use { | ||
dir := fmt.Sprintf("%v/%v", filepath.Dir(abs), use.DiskPath) | ||
batch = append(batch, ActionModules(ModuleOpts{Direct: true, Indirect: true}).Chdir(dir)) | ||
} | ||
return batch.ToA() | ||
}) | ||
}) | ||
} |