Skip to content

Commit df4f8ce

Browse files
authored
Merge pull request #12 from carapace-sh/local-actions
copied ActionProfiles and ActionRegions from carapace-bin
2 parents af17c66 + 9f4f736 commit df4f8ce

6 files changed

Lines changed: 117 additions & 5 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Carapace-aws is an enriched completer for [aws-cli].
77
## How it works
88

99
The subcommands in [aws-cli] are mostly based on the [botocore] service definitions.
10-
These are `json` files that contain additional information and static completions.
10+
Which are `json` files that contain additional information and static completions.
1111
But these are not fully exposed to the shell completion and only accessible in [auto-prompt].
1212

1313
Carapace-aws parses the [botocore] service definitions into a [carapace] based completer.
@@ -16,7 +16,7 @@ Undefined completions are simply delegated to the official [aws_completer].
1616

1717
## Getting Started
1818

19-
When using [carapace-bin] this happens implicitly when the `carapace-aws` binary is in [PATH].
19+
When using [carapace-bin] `carapace-aws` is implicitly used when the binary is in [PATH].
2020

2121
Otherwise the completions can be loaded directly with [`carapace-aws _carapace`](https://carapace-sh.github.io/carapace/carapace/gen.html#hidden-subcommand).
2222

cmd/carapace-aws/cmd/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/carapace-sh/carapace"
77
"github.com/carapace-sh/carapace-aws/cmd/carapace-aws/cmd/botocore"
88
"github.com/carapace-sh/carapace-aws/cmd/carapace-aws/cmd/common"
9+
_ "github.com/carapace-sh/carapace-aws/pkg/actions"
10+
"github.com/carapace-sh/carapace-aws/pkg/actions/aws"
911
spec "github.com/carapace-sh/carapace-spec"
1012
"github.com/carapace-sh/carapace/pkg/style"
1113
"github.com/spf13/cobra"
@@ -49,8 +51,8 @@ func init() {
4951
),
5052
"color": carapace.ActionValues("on", "off", "auto").StyleF(style.ForKeyword),
5153
"output": carapace.ActionValues("json", "text", "table", "yaml", "yaml-stream"),
52-
"profile": spec.ActionMacro("$carapace.tools.aws.Profiles"),
53-
"region": spec.ActionMacro("$carapace.tools.aws.Regions"),
54+
"profile": aws.ActionProfiles(),
55+
"region": aws.ActionRegions(),
5456
})
5557

5658
for name, description := range botocore.Services() {
@@ -110,4 +112,6 @@ func init() {
110112
common.ActionBridgeAwsCompleter(),
111113
)
112114
}
115+
116+
spec.Register(rootCmd)
113117
}

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ require (
77
github.com/carapace-sh/carapace-spec v1.3.5
88
github.com/spf13/cobra v1.10.1
99
github.com/spf13/pflag v1.0.10
10+
gopkg.in/ini.v1 v1.67.0
11+
gopkg.in/yaml.v3 v3.0.1
1012
)
1113

1214
require (
1315
github.com/carapace-sh/carapace-shlex v1.1.1 // indirect
1416
github.com/inconshreveable/mousetrap v1.1.0 // indirect
15-
gopkg.in/yaml.v3 v3.0.1 // indirect
17+
github.com/stretchr/testify v1.11.1 // indirect
1618
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ github.com/carapace-sh/carapace-shlex v1.1.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0Xp
55
github.com/carapace-sh/carapace-spec v1.3.5 h1:F/OS1CQciwTtyZbaPsOz5jQwtzbjHUoPCjCClELbb+s=
66
github.com/carapace-sh/carapace-spec v1.3.5/go.mod h1:sLWIRftQoRogZj8xdDYqnZb8K4N0/YbEj03m2C5R8ZA=
77
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
8+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
9+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
810
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
911
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
12+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
13+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1014
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
1115
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
1216
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
@@ -15,7 +19,11 @@ github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
1519
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1620
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
1721
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
22+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
23+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
1824
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1925
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
26+
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
27+
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
2028
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2129
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/actions/actions.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package actions
2+
3+
import (
4+
"github.com/carapace-sh/carapace-aws/pkg/actions/aws"
5+
spec "github.com/carapace-sh/carapace-spec"
6+
)
7+
8+
func init() {
9+
// TODO create with go:generate
10+
spec.AddMacro("Profiles", spec.MacroN(aws.ActionProfiles))
11+
spec.AddMacro("Regions", spec.MacroN(aws.ActionRegions))
12+
}

pkg/actions/aws/aws.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// package aws contains amazon web services related actions
2+
package aws
3+
4+
import (
5+
"strings"
6+
7+
"github.com/carapace-sh/carapace"
8+
"gopkg.in/ini.v1"
9+
)
10+
11+
// ActionRegions completes region names
12+
//
13+
// eu-south-1 (Europe Milan)
14+
// ap-east-1 (Asia Pacific Hong Kong)
15+
func ActionRegions() carapace.Action {
16+
return carapace.ActionValuesDescribed(
17+
"af-south-1", "Africa (Cape Town)",
18+
"ap-east-1", "Asia Pacific (Hong Kong)",
19+
"ap-east-2", "Asia Pacific (Taipei)",
20+
"ap-northeast-1", "Asia Pacific (Tokyo)",
21+
"ap-northeast-2", "Asia Pacific (Seoul)",
22+
"ap-northeast-3", "Asia Pacific (Osaka)",
23+
"ap-south-1", "Asia Pacific (Mumbai)",
24+
"ap-south-2", "Asia Pacific (Hyderabad)",
25+
"ap-southeast-1", "Asia Pacific (Singapore)",
26+
"ap-southeast-2", "Asia Pacific (Sydney)",
27+
"ap-southeast-3", "Asia Pacific (Jakarta)",
28+
"ap-southeast-4", "Asia Pacific (Melbourne)",
29+
"ap-southeast-5", "Asia Pacific (Malaysia)",
30+
"ap-southeast-6", "Asia Pacific (New Zealand)",
31+
"ap-southeast-7", "Asia Pacific (Thailand)",
32+
"ca-central-1", "Canada (Central)",
33+
"ca-west-1", "Canada West (Calgary)",
34+
"eu-central-1", "Europe (Frankfurt)",
35+
"eu-central-2", "Europe (Zurich)",
36+
"eu-north-1", "Europe (Stockholm)",
37+
"eu-south-1", "Europe (Milan)",
38+
"eu-south-2", "Europe (Spain)",
39+
"eu-west-1", "Europe (Ireland)",
40+
"eu-west-2", "Europe (London)",
41+
"eu-west-3", "Europe (Paris)",
42+
"il-central-1", "Israel (Tel Aviv)",
43+
"me-central-1", "Middle East (UAE)",
44+
"me-south-1", "Middle East (Bahrain)",
45+
"mx-central-1", "Mexico (Central)",
46+
"sa-east-1", "South America (São Paulo)",
47+
"us-east-1", "US East (N. Virginia)",
48+
"us-east-2", "US East (Ohio)",
49+
"us-west-1", "US West (N. California)",
50+
"us-west-2", "US West (Oregon)",
51+
)
52+
}
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)