-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacker.go
180 lines (155 loc) · 4.54 KB
/
stacker.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
This is the stacker command.
Author: [email protected]
Copyright 2024, all rights reserved
*/
package main
import (
"flag"
"fmt"
gs "github.com/jceaser/stacker/grpc"
"github.com/jceaser/stacker/stacker"
"os"
"path/filepath"
)
/* ************************************************************************** */
//MARK: - Structs
/*
Holds all application configuration and state, can be passed to lower
functions if needed
*/
type AppData struct {
peekMode *bool
listMode *bool
updateMode *bool
deleteMode *bool
rotateMode *bool
clearMode *bool
versionMode *bool
http *bool
rpcd *bool
rpc *string
host *string
port *int
cleanLimit *int
encode *bool
name *string
path *string
}
func (self AppData) Context() stacker.Config {
return stacker.Config{
Name: *self.name,
Path: *self.path,
Encode: *self.encode,
Host: *self.host,
Port: *self.port}
}
/* ************************************************************************** */
//MARK: - Server
func host(config stacker.Config) {
stacker.Host(config)
}
/* ************************************************************************** */
//MARK: - Functions
/*
export XDG_CONFIG_HOME=~/.config/test ; go run stacker.go
*/
func FindConfigPath() string {
configHome := os.Getenv("XDG_CONFIG_HOME") //standard location
appConfigPath := filepath.Base(os.Args[0]) + "/data.json" //sub directory
if len(configHome) < 1 {
configHome = "~/.config" //assume things
}
relativeConfigPath := fmt.Sprintf("%s/%s", configHome, appConfigPath)
configPath := stacker.ExpandPath(relativeConfigPath)
return configPath
}
/* create app data from the command line flags */
func Setup() AppData {
app_data := AppData{}
//action flags
app_data.peekMode = flag.Bool("peek", false, "peek mode")
app_data.listMode = flag.Bool("ls", false, "List Everything")
app_data.updateMode = flag.Bool("update", false, "Update an item")
app_data.deleteMode = flag.Bool("delete", false, "Delete item")
app_data.rotateMode = flag.Bool("rotate", false, "Rotate up")
app_data.clearMode = flag.Bool("clear", false, "Clear all data")
app_data.versionMode = flag.Bool("version", false, "Clear all data")
//networking flags
app_data.http = flag.Bool("http", false, "Create HTTP host")
app_data.rpcd = flag.Bool("rpcd", false, "Create RPC host")
app_data.rpc = flag.String("rpc", "", "Use the RPC client")
app_data.host = flag.String("host", "localhost", "The server port")
app_data.port = flag.Int("port", 54321, "The server port")
//assorted flags
app_data.cleanLimit = flag.Int("clean", -1, "Clean old data")
app_data.encode = flag.Bool("encode", false, "Mask sensitive data in list and file")
app_data.name = flag.String("name", "default", "Stack Name")
app_data.path = flag.String("path", "", "full Path to stack file")
flag.Parse()
//default flag value
if len(*app_data.path) < 1 {
default_path := FindConfigPath()
app_data.path = &default_path
}
return app_data
}
/*
Primary action for the app, if a stream exists, then push it to the stack, if on
stream then pop from the stack.
*/
func StreamAction(app_data AppData) {
/*
Default action, if standard in has text, then assume we are in write mode
otherwise assume read mode.
*/
config := app_data.Context()
text := stacker.ReadFromStandardIn()
if len(text) > 0 {
//Write mode
if *app_data.updateMode {
fmt.Println(stacker.UpdateItem(config, text))
} else {
stacker.CreateItem(config, text)
}
} else {
//no data, so fall back to a read
fmt.Println(stacker.ReadItem(config))
}
}
func main() {
app_data := Setup()
if *app_data.versionMode {
fmt.Println("stacker 1.0 by [email protected]")
fmt.Printf("Config file: [%s]\n", *app_data.path)
os.Exit(0)
}
config := app_data.Context()
//Start either the HTML server, the GRCP server, or GRCP client
if *app_data.http {
host(config)
return
} else if *app_data.rpcd {
gs.Server(config)
return
} else if len(*app_data.rpc) > 0 {
gs.Client(config, *app_data.rpc)
return
}
//local command line mode supports a slightly expanded command set
if *app_data.listMode {
fmt.Print(stacker.ListItems(config))
} else if *app_data.peekMode {
fmt.Println(stacker.PeekItem(config))
} else if *app_data.deleteMode {
fmt.Println(stacker.DeleteItem(config))
} else if *app_data.clearMode {
stacker.ClearAll(config)
} else if *app_data.rotateMode {
fmt.Println(stacker.RotateUp(config))
} else if *app_data.cleanLimit > 0 {
stacker.RemoveOld(config, *app_data.cleanLimit)
} else {
StreamAction(app_data)
}
}