-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftabcmd.go
112 lines (91 loc) · 2.24 KB
/
conftabcmd.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2017, 2018 Damon Revoe. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"path"
"github.com/spf13/cobra"
)
func editConftab() error {
workspaceDir, err := getWorkspaceDir()
if err != nil {
return err
}
editor := os.Getenv("VISUAL")
if editor == "" {
editor = os.Getenv("EDITOR")
}
if editor == "" {
return errors.New("neither $VISUAL nor $EDITOR is set")
}
privateDir := getPrivateDir(workspaceDir)
conftabPathname := path.Join(privateDir, conftabFilename)
origConftab, err := readConftab(conftabPathname)
if err != nil {
return err
}
editorCmd := exec.Command(editor, conftabPathname)
editorCmd.Stdin = os.Stdin
editorCmd.Stdout = os.Stdout
editorCmd.Stderr = os.Stderr
err = editorCmd.Run()
if err != nil {
return errors.New(editor + ": " + err.Error())
}
updatedConftab, err := readConftab(conftabPathname)
if err != nil {
return err
}
deletedSections, changedSections, addedSections :=
origConftab.diff(updatedConftab)
if len(deletedSections) == 0 &&
len(changedSections) == 0 &&
len(addedSections) == 0 {
fmt.Println("No effective changes detected")
return nil
}
for _, pkgName := range deletedSections {
fmt.Println("Deleted section: [" + pkgName + "]")
}
for _, pkgName := range addedSections {
fmt.Println("New section: [" + pkgName + "]")
}
for pkgName, changes := range changedSections {
fmt.Println("Changes in [" + pkgName + "]:")
for _, chg := range changes {
if chg.added != "" {
if chg.deleted != "" {
fmt.Println(chg.added +
" (changed from " +
chg.deleted + ")")
} else {
fmt.Println(chg.added + " (added)")
}
} else if chg.deleted != "" {
fmt.Println("# " + chg.deleted + " (deleted)")
}
}
}
return nil
}
var conftabCmdName = "conftab"
var conftabCmd = &cobra.Command{
Use: conftabCmdName,
Short: "Edit the conftab file",
Args: cobra.MaximumNArgs(0),
Run: func(_ *cobra.Command, _ []string) {
if err := editConftab(); err != nil {
log.Fatal(err)
}
},
}
func init() {
rootCmd.AddCommand(conftabCmd)
conftabCmd.Flags().SortFlags = false
addWorkspaceDirFlag(conftabCmd)
}