Skip to content

Commit 46ec77e

Browse files
authored
Merge pull request #147 from carapace-sh/use-bridge
use bridge action
2 parents 2870397 + f4d9177 commit 46ec77e

4 files changed

Lines changed: 45 additions & 65 deletions

File tree

cmd/carapace-aws/cmd/common/bridge.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,14 @@ package common
22

33
import (
44
"os"
5-
"strings"
65

76
"github.com/carapace-sh/carapace"
8-
"github.com/carapace-sh/carapace/pkg/style"
7+
"github.com/carapace-sh/carapace-bridge/pkg/actions/bridge"
98
)
109

1110
func ActionBridgeAwsCompleter() carapace.Action {
1211
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
13-
c.Setenv("COMP_LINE", "aws "+strings.Join(append(os.Args[3:], c.Value), " ")) // TODO escape/quote special characters
14-
return carapace.ActionExecCommand("aws_completer")(func(output []byte) carapace.Action {
15-
lines := strings.Split(string(output), "\n")
16-
if lines[0] == "" {
17-
return carapace.ActionValues()
18-
}
19-
for index, line := range lines {
20-
if strings.HasSuffix(line, " ") {
21-
lines[index] = strings.TrimSuffix(line, " ") // v1 has space suffix
22-
}
23-
}
24-
a := carapace.ActionValues(lines[:len(lines)-1]...)
25-
switch {
26-
case strings.HasPrefix(c.Value, "file://"):
27-
a = a.NoSpace('/').StyleF(func(s string, sc style.Context) string {
28-
return style.ForPath(strings.TrimPrefix(s, "file://"), sc)
29-
})
30-
case strings.HasPrefix(c.Value, "fileb://"):
31-
a = a.NoSpace('/').StyleF(func(s string, sc style.Context) string {
32-
return style.ForPath(strings.TrimPrefix(s, "fileb://"), sc)
33-
})
34-
}
35-
return a
36-
}).Invoke(c).ToA()
12+
c.Args = carapace.NewContext(os.Args[4:]...).Args // TODO nasty args passthrough
13+
return bridge.ActionAws("aws").Invoke(c).ToA()
3714
})
3815
}

cmd/carapace-aws/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func init() {
7777

7878
carapace.Gen(operationCmd).PreInvoke(func(cmd *cobra.Command, flag *pflag.Flag, action carapace.Action) carapace.Action {
7979
// TODO same for deeper wait subcommands
80-
if flag != nil && flag.Value.Type() == "string" {
80+
if flag != nil && flag.Value.Type() != "bool" {
8181
if _, ok := subCmd.Completion.Flag[flag.Name]; !ok {
8282
return common.ActionBridgeAwsCompleter()
8383
}

pkg/actions/aws/profile.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package aws
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"gopkg.in/ini.v1"
6+
"strings"
7+
)
8+
9+
// ActionProfiles completes configuration profile names
10+
//
11+
// someprofile (eu-central-1)
12+
// anotherprofile (us-east-1)
13+
func ActionProfiles() carapace.Action {
14+
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
15+
profiles := []string{}
16+
17+
// TODO support windows
18+
if path, err := c.Abs("~/.aws/config"); err != nil {
19+
return carapace.ActionMessage(err.Error())
20+
} else {
21+
if cfg, err := ini.Load(path); err != nil {
22+
return carapace.ActionMessage(err.Error())
23+
} else {
24+
for _, section := range cfg.Sections() {
25+
if after, ok := strings.CutPrefix(section.Name(), "profile "); ok {
26+
profiles = append(profiles, after)
27+
if key, err := section.GetKey("region"); err != nil {
28+
profiles = append(profiles, "")
29+
} else {
30+
profiles = append(profiles, key.String())
31+
}
32+
}
33+
}
34+
if len(profiles) == 0 {
35+
profiles = append(profiles, "default", "")
36+
}
37+
return carapace.ActionValuesDescribed(profiles...)
38+
}
39+
}
40+
})
41+
}
Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
// package aws contains amazon web services related actions
21
package aws
32

43
import (
5-
"strings"
6-
74
"github.com/carapace-sh/carapace"
8-
"gopkg.in/ini.v1"
95
)
106

117
// ActionRegions completes region names
@@ -50,37 +46,3 @@ func ActionRegions() carapace.Action {
5046
"us-west-2", "US West (Oregon)",
5147
)
5248
}
53-
54-
// ActionProfiles completes configuration profile names
55-
//
56-
// someprofile (eu-central-1)
57-
// anotherprofile (us-east-1)
58-
func ActionProfiles() carapace.Action {
59-
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
60-
profiles := []string{}
61-
62-
// TODO support windows
63-
if path, err := c.Abs("~/.aws/config"); err != nil {
64-
return carapace.ActionMessage(err.Error())
65-
} else {
66-
if cfg, err := ini.Load(path); err != nil {
67-
return carapace.ActionMessage(err.Error())
68-
} else {
69-
for _, section := range cfg.Sections() {
70-
if after, ok := strings.CutPrefix(section.Name(), "profile "); ok {
71-
profiles = append(profiles, after)
72-
if key, err := section.GetKey("region"); err != nil {
73-
profiles = append(profiles, "")
74-
} else {
75-
profiles = append(profiles, key.String())
76-
}
77-
}
78-
}
79-
if len(profiles) == 0 {
80-
profiles = append(profiles, "default", "")
81-
}
82-
return carapace.ActionValuesDescribed(profiles...)
83-
}
84-
}
85-
})
86-
}

0 commit comments

Comments
 (0)