-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (62 loc) · 2.11 KB
/
main.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
package main
import (
"fmt"
"os"
"strings"
"github.com/bharatkalluri/harmony/config"
"github.com/bharatkalluri/harmony/models"
)
func GetShellTypeFromStr(shellTypeStr string) models.Shell {
if strings.Compare(shellTypeStr, "bash") == 0 {
fmt.Println("Bash support is coming soon!")
os.Exit(1)
return models.BashShell{}
} else if strings.Compare(shellTypeStr, "zsh") == 0 {
return models.ZSHShell{}
} else {
panic("SHELL_TYPE needs to be either bash or zsh for now")
}
}
func initiateSync(shellType string) {
shell := GetShellTypeFromStr(shellType)
shellHistoryGist, _ := models.NewShellHistoryGist()
onlineHistory, err := shellHistoryGist.GetShellHistoryFromGist()
if err != nil {
panic("Failed to retrieve history from gist")
}
localHistory, err := models.GetShellHistory(shell)
if err != nil {
fmt.Printf("Failed to retrieve local history: %s\n", err)
}
fmt.Printf("Found %d entries online\n", len(onlineHistory.History))
fmt.Printf("Found %d entries locally\n", len(localHistory.History))
mergedShellHistory := models.MergeShellHistories(onlineHistory, localHistory)
fmt.Printf("Merged and Uploading %d entries, found %d common\n", len(mergedShellHistory.History), (len(onlineHistory.History)+len(localHistory.History))-len(mergedShellHistory.History))
err = models.WriteLocalShellHistory(mergedShellHistory, shell)
if err != nil {
panic("Writing history to local has failed!")
}
err = shellHistoryGist.UpsertShellHistoryGist(mergedShellHistory)
if err != nil {
panic("Writing history to gist has failed!")
}
fmt.Println("Sync complete!")
}
func main() {
appConfig, err := config.ReadAppConfig()
args := os.Args[1:]
if len(args) == 0 {
if err == config.MissingConfigFileError {
fmt.Println("Hey there! I could not find a config file over at ~/.config/harmony/config")
fmt.Println("Use `harmony config` to set up the configuration file")
fmt.Println("For more detailed explanation, visit https://github.com/BharatKalluri/harmony/blob/master/README.md")
os.Exit(0)
return
}
initiateSync(appConfig.ShellType)
return
}
if args[0] == "configure" {
config.WriteAppConfig()
}
}