Skip to content

Commit 9a3a1ad

Browse files
minor bug fixes
1 parent c8ae7ab commit 9a3a1ad

File tree

9 files changed

+96
-35
lines changed

9 files changed

+96
-35
lines changed

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
name: Set up Go
2222
uses: actions/setup-go@v2
2323
with:
24-
go-version: 1.19
24+
go-version: 1.20
2525
-
2626
name: Run GoReleaser
2727
uses: goreleaser/goreleaser-action@v2

cmd/completion.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"fmt"
8+
"os"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// completionCmd represents the completion command
14+
var completionCmd = &cobra.Command{
15+
Use: "completion [bash|zsh|fish|powershell]",
16+
Short: "Generate completion script",
17+
Long: `To load completions`,
18+
DisableFlagsInUseLine: true,
19+
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
20+
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
21+
Run: func(cmd *cobra.Command, args []string) {
22+
fmt.Println("completion called")
23+
switch args[0] {
24+
case "bash":
25+
cmd.Root().GenBashCompletion(os.Stdout)
26+
case "zsh":
27+
cmd.Root().GenZshCompletion(os.Stdout)
28+
case "fish":
29+
cmd.Root().GenFishCompletion(os.Stdout, true)
30+
case "powershell":
31+
cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
32+
}
33+
},
34+
}
35+
36+
func init() {
37+
rootCmd.AddCommand(completionCmd)
38+
39+
// Here you will define your flags and configuration settings.
40+
41+
// Cobra supports Persistent Flags which will work for this command
42+
// and all subcommands, e.g.:
43+
// completionCmd.PersistentFlags().String("foo", "", "A help for foo")
44+
45+
// Cobra supports local flags which will only run when this command
46+
// is called directly, e.g.:
47+
// completionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
48+
}

cmd/connect.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ var connectCmd = &cobra.Command{
2323
Example:
2424
sshm connect 1
2525
sshm connect fb-stage`,
26+
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
27+
fmt.Println("in valid func")
28+
if len(args) != 0 {
29+
return nil, cobra.ShellCompDirectiveNoFileComp
30+
}
31+
return getConnection(toComplete), cobra.ShellCompDirectiveNoFileComp
32+
},
2633
Run: func(cmd *cobra.Command, args []string) {
2734
fmt.Println("connect called")
2835
// command := exec.Command("ssh", "[email protected]")
@@ -48,7 +55,7 @@ var connectCmd = &cobra.Command{
4855
command.Stderr = os.Stderr
4956
e := command.Run()
5057
if e != nil {
51-
log.Fatal(err)
58+
log.Fatal(e)
5259
}
5360

5461
},

cmd/delete.go

-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ Example:
2323

2424
index, err := strconv.Atoi(args[0])
2525
if err == nil && index < len(SshList.List) {
26-
27-
// SshList.List = append(SshList.List[:index], SshList.List[index+1:]...)
28-
// s[i] = s[len(s)-1]
29-
// return s[:len(s)-1]
3026
SshList.List[index] = SshList.List[len(SshList.List)-1]
3127
fmt.Println(SshList.List[index].Project, SshList.List[index].Env, "deleted")
3228
SshList.List = SshList.List[:len(SshList.List)-1]

cmd/helper.go

+21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"log"
99
"os"
10+
"regexp"
1011
)
1112

1213
func LoadData() ConList {
@@ -68,3 +69,23 @@ func SaveData(data ConList) {
6869
}
6970

7071
}
72+
73+
func getConnection(data string) []string {
74+
// fmt.Println("in getConnection")
75+
keys := []string{}
76+
for k, _ := range SshShortMap {
77+
78+
m, err := regexp.MatchString(`.*`+data+`.*`, k)
79+
if err != nil {
80+
fmt.Println(err)
81+
}
82+
if m {
83+
// fmt.Println("found: ", m, k)
84+
85+
keys = append(keys, k)
86+
}
87+
}
88+
fmt.Println(keys)
89+
90+
return keys
91+
}

cmd/list.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ var listCmd = &cobra.Command{
1818
Long: `This command will list your ssh connections list. For example:
1919
2020
sshm list`,
21+
// ValidArgs: []string{"fb", "google"},
2122
Run: func(cmd *cobra.Command, args []string) {
2223
// fmt.Println("list called")
23-
24+
// getConnection("st")
2425
table := tablewriter.NewWriter(os.Stdout)
2526
table.SetHeader([]string{"Index", "Project", "Env", "Shrotcut", "Con"})
2627
table.SetAutoWrapText(false)

cmd/root.go

+12-26
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
/*
2-
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
2+
Copyright © 2022 Dhruv <[email protected]>
33
*/
44
package cmd
55

66
import (
7-
"fmt"
87
"os"
9-
"path"
10-
"runtime"
118

129
"github.com/spf13/cobra"
1310
)
@@ -26,40 +23,27 @@ type ConList struct {
2623

2724
var SshIndexMap = make(map[int]SshCon)
2825
var SshShortMap = make(map[string]SshCon)
29-
var Version = "development"
26+
var Version = "v1.1.7"
3027

3128
var DATA_PATH string = ""
3229
var SshList ConList
3330

3431
// rootCmd represents the base command when called without any subcommands
3532
var rootCmd = &cobra.Command{
36-
Use: "sshm",
37-
Short: "sshm is a simple cli tool to manage your ssh connections",
38-
Long: `sshm is a simple cli tool to manage your ssh connections. You can add,edit and view the connection strings.`,
39-
// Uncomment the following line if your bare application
40-
// has an action associated with it:
41-
// Run: func(cmd *cobra.Command, args []string) { },
33+
Use: "sshm",
34+
Short: "sshm is a simple cli tool to manage your ssh connections",
35+
Long: `sshm is a simple cli tool to manage your ssh connections. You can add,edit and delete the connections.`,
36+
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
4237
}
4338

44-
// Execute adds all child commands to the root command and sets flags appropriately.
4539
// This is called by main.main(). It only needs to happen once to the rootCmd.
4640
func Execute() {
47-
mydir, erri := os.Getwd()
48-
if erri != nil {
49-
fmt.Println(erri)
50-
}
51-
fmt.Println("current working dir is : ", mydir)
52-
_, filename, _, ok := runtime.Caller(0)
53-
if !ok {
54-
panic("No caller information")
55-
}
56-
// fmt.Println(version)
57-
// fmt.Printf("Filename : %q, Dir : %q\n", filename, path.Dir(filename))
58-
fmt.Println(path.Dir(filename))
59-
// DATA_PATH = path.Dir(filename) + "/data.json"
60-
DATA_PATH = "./data.json"
41+
dataPath := "./data.json"
42+
43+
DATA_PATH = dataPath
6144

6245
SshList = LoadData()
46+
// fmt.Println(SshIndexMap, SshShortMap)
6347
err := rootCmd.Execute()
6448
if err != nil {
6549
os.Exit(1)
@@ -76,4 +60,6 @@ func init() {
7660
// Cobra also supports local flags, which will only run
7761
// when this action is called directly.
7862
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
63+
// rootCmd.CompletionOptions.DisableDefaultCmd = true
64+
7965
}

data.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"List":[]
3+
}

main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3-
2+
Copyright © 2022 Dhruv <[email protected]>
43
*/
54
package main
65

0 commit comments

Comments
 (0)