Skip to content

Commit

Permalink
added command completion
Browse files Browse the repository at this point in the history
  • Loading branch information
neel-bp committed Sep 3, 2023
1 parent bfc312b commit f4c2bb0
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 3 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/atotto/clipboard v0.1.4
github.com/go-git/go-git/v5 v5.4.2
github.com/olekukonko/tablewriter v0.0.5
github.com/posener/complete/v2 v2.1.0
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1
)
Expand All @@ -23,6 +24,7 @@ require (
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/posener/script v1.2.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,19 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete/v2 v2.1.0 h1:IpAWxMyiJ6zDSoq+QmEBF0thpOramC0kYuEFBTcQeTI=
github.com/posener/complete/v2 v2.1.0/go.mod h1:AkzsSVGx4ysH/4OhZf57dr4yszGXgFmXsP/VNwlaW7U=
github.com/posener/script v1.2.0 h1:DrZz0qFT8lCLkYNi1PleLDANFnKxJ2VmlNPJbAkVLsE=
github.com/posener/script v1.2.0/go.mod h1:s4sVvRXtdc/1aK6otTSeW2BVXndO8MsoOVUwK74zcg4=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down Expand Up @@ -108,5 +112,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func main() {
src.GenerateCompletion()
err := src.CommandHandler(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "\n%v\n", err)
Expand Down
86 changes: 86 additions & 0 deletions src/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package src

import (
"fmt"

"github.com/posener/complete/v2"
"github.com/posener/complete/v2/predict"
)

func numbers() []string {
res := make([]string, 0)
for i := 0; i < 24; i++ {
res = append(res, fmt.Sprintf("%v", i))
}
return res
}

type ServiceSitePredictor struct{}

func (ServiceSitePredictor) Predict(prefix string) []string {
res, _ := StoreOptionsForCompletionPredictor(prefix)
return res
}

func GenerateCompletion() {

serviceSitePredictor := new(ServiceSitePredictor)

storecmd := &complete.Command{
Flags: map[string]complete.Predictor{
"w": predict.Nothing,
"sc": predict.Nothing,
"gp": predict.Set(numbers()),
},
}

getcmd := &complete.Command{
Flags: map[string]complete.Predictor{
"clip": predict.Nothing,
},
Args: serviceSitePredictor,
}

deletecmd := &complete.Command{
Args: serviceSitePredictor,
}

listcmd := &complete.Command{
Flags: map[string]complete.Predictor{
"u": predict.Nothing,
"p": predict.Nothing,
},
}

gitauthcmd := &complete.Command{
Flags: map[string]complete.Predictor{
"w": predict.Nothing,
},
}

helpcmd := &complete.Command{
Args: predict.Set{
"store",
"get",
"delete",
"list",
"gitauth",
"pull",
"push",
},
}

cmd := &complete.Command{
Sub: map[string]*complete.Command{
"store": storecmd,
"get": getcmd,
"delete": deletecmd,
"list": listcmd,
"gitauth": gitauthcmd,
"pull": {},
"push": {},
"help": helpcmd,
},
}
cmd.Complete("gpman")
}
2 changes: 1 addition & 1 deletion src/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Options:
and passphrase (passphrase is used to encrypt given values)
-gp [INT] generates a password of given length and stores it as
password against given site/service, when given password prompt is skipped in wizard
password against given site/service, when given, password prompt is skipped in wizard
if wizard flag (-w) is not present password argument is no longer needed
generated password is considered instead.
Expand Down
27 changes: 27 additions & 0 deletions src/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"math/big"
"os"
"path/filepath"
"strings"

"github.com/andreburgaud/crypt2go/ecb"
"github.com/andreburgaud/crypt2go/padding"
Expand Down Expand Up @@ -340,3 +341,29 @@ func ListPasses(passphrase string, user_flag, pass_flag bool) ([][]string, error
return data, nil

}

func StoreOptionsForCompletionPredictor(prefix string) ([]string, error) {
res := make([]string, 0)
home, err := os.UserHomeDir()
if err != nil {
return res, err
}
JSON_FILE := filepath.Join(home, VAULT_DIR, VAULT_JSON)
content, err := ioutil.ReadFile(JSON_FILE)
if err != nil {
return res, err
}
unmarsheled := make(map[string]interface{}, 0)
err = json.Unmarshal(content, &unmarsheled)
if err != nil {
return res, err
}

for k := range unmarsheled {
if strings.HasPrefix(strings.ToLower(k), strings.ToLower(prefix)) {
res = append(res, k)
}
}
return res, nil

}

0 comments on commit f4c2bb0

Please sign in to comment.