/
update.go
55 lines (51 loc) · 1.2 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cmd
import (
"fmt"
"github.com/akymos/ff/internal"
"github.com/spf13/cobra"
"os"
)
var (
updateCmd = &cobra.Command{
Use: "update [alias] [path]",
Aliases: []string{"upd", "u"},
Short: "Update a directory alias",
Long: `Update a directory alias.
Arguments:
[alias] required. The name of the alias.
[path] optional. It is the path to the directory to be aliased. Default is the current directory.`,
Example: `$ ff add alias_name /tmp
$ ff update alias_name /var`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
defer internal.BaseConfig.Db.Close()
alias := args[0]
path := ""
if len(args) > 1 {
path = args[1]
}
if path == "" || path == "." || path == "./" {
p, err := os.Getwd()
if err != nil {
return err
}
path = p
}
err := internal.Update(alias, path)
if err != nil {
return err
}
err = internal.PopulateAlias()
if err != nil {
return err
}
fmt.Printf("Alias %s updated to %s.\n", alias, path)
fmt.Printf("Now run \n%s\nor restart the shell.\n", "source \"$(ff alias)\"")
internal.CheckNewVersion()
return nil
},
}
)
func init() {
rootCmd.AddCommand(updateCmd)
}