/
reset.go
71 lines (64 loc) · 1.81 KB
/
reset.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
package cmd
import (
"fmt"
"github.com/briandowns/spinner"
_ "github.com/mattn/go-sqlite3"
"io/ioutil"
"os"
"regexp"
//"strings"
"time"
"github.com/spf13/cobra"
)
// resetCmd represents the remove command
var resetCmd = &cobra.Command{
Use: "reset",
Aliases: []string{"ua"},
Short: "Remove every keys",
// TODO: add a long description
Long: `Remove every keys from the keyfile`,
Run: func(cmd *cobra.Command, args []string) {
// starts... the spinner
spinner := spinner.New(spinner.CharSets[14], 50*time.Millisecond)
spinner.Start()
re := regexp.MustCompile("#-- Akmey START --\n((?:.|\n)+)\n#-- Akmey STOP --")
db, err := initFileDB(getStoragePath(), keyfile)
defer db.Close()
tx, err := db.Begin()
cfe(err)
tx.Exec("delete from users")
cfe(err)
tx.Exec("delete from keys")
cfe(err)
// Step 1 : Fetch installed keys
dat, err := ioutil.ReadFile(keyfile)
newContent := ""
cfe(err)
match := re.FindStringSubmatch(string(dat))
if match == nil {
fmt.Println("Akmey is not present in this file")
os.Exit(0)
}
/* for _, torm := range toberemoved {
if newContent == "" {
newContent = strings.Replace(string(dat), match[1], torm, -1)
} else {
newContent = strings.Replace(newContent, match[1], torm, -1)
}
} */
err = ioutil.WriteFile(keyfile, []byte(newContent), 0)
cfe(err)
tx.Commit()
fmt.Println("\n")
},
}
func init() {
rootCmd.AddCommand(resetCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// resetCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// resetCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}