refactor(config): change config location and keyword names
This commit is contained in:
parent
5a154429d8
commit
ebc11dd507
1 changed files with 90 additions and 82 deletions
162
main.go
162
main.go
|
|
@ -7,6 +7,7 @@ import (
|
|||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -18,14 +19,14 @@ import (
|
|||
type Config struct {
|
||||
SSHUser string `mapstructure:"SSH_USER"`
|
||||
SSHHost string `mapstructure:"SSH_HOST"`
|
||||
VardaUser string `mapstructure:"VARDA_USER"`
|
||||
VardaHost string `mapstructure:"VARDA_HOST"`
|
||||
LouHost string `mapstructure:"LOU_HOST"`
|
||||
LouUser string `mapstructure:"LOU_USER"`
|
||||
LouMac string `mapstructure:"LOU_MAC"`
|
||||
JumpUser string `mapstructure:"JUMP_USER"`
|
||||
JumpHost string `mapstructure:"JUMP_HOST"`
|
||||
WorkstationHost string `mapstructure:"WORKSTATION_HOST"`
|
||||
WorkstationUser string `mapstructure:"WWORKSTATION_USER"`
|
||||
WorkstationMac string `mapstructure:"WORKSTATION_MAC"`
|
||||
RDPUser string `mapstructure:"RDP_USER"`
|
||||
RDPPassword string `mapstructure:"RDP_PASSWORD"`
|
||||
LouIP string `mapstructure:"LOU_IP"`
|
||||
WorkstationIP string `mapstructure:"WORKSTATION_IP"`
|
||||
SSHPort int `mapstructure:"SSH_PORT"`
|
||||
}
|
||||
|
||||
|
|
@ -33,12 +34,19 @@ var cfg Config
|
|||
|
||||
func init() {
|
||||
log.Println("Loading Config File")
|
||||
viper.SetConfigFile("work-config.toml")
|
||||
configPath, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
workConfigPath := filepath.Join(configPath, "work")
|
||||
configFile := filepath.Join(workConfigPath, "config.toml")
|
||||
viper.SetConfigFile(configFile)
|
||||
viper.SetConfigType("toml")
|
||||
viper.AddConfigPath(".")
|
||||
viper.AutomaticEnv()
|
||||
|
||||
err := viper.ReadInConfig()
|
||||
err = viper.ReadInConfig()
|
||||
if err != nil {
|
||||
log.Printf("error loading config file: %v", err)
|
||||
}
|
||||
|
|
@ -51,68 +59,6 @@ func init() {
|
|||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// err := LoadConfig()
|
||||
// if err != nil {
|
||||
// log.Printf("error: %v", err)
|
||||
// }
|
||||
var choice string
|
||||
form := huh.NewForm(
|
||||
huh.NewGroup(
|
||||
huh.NewSelect[string]().
|
||||
Title("What would you like to do?").
|
||||
Options(
|
||||
huh.NewOption("Start", "Start"),
|
||||
huh.NewOption("Stop Work", "stop Work"),
|
||||
huh.NewOption("Show Week Summary", "show week summary"),
|
||||
huh.NewOption("Show Month Summary", "show month summary"),
|
||||
huh.NewOption("Start Break", "start break"),
|
||||
huh.NewOption("Stop Break", "stop break"),
|
||||
huh.NewOption("Export Timetable", "export"),
|
||||
huh.NewOption("Wake Lou", "wake lou"),
|
||||
huh.NewOption("Connect to Varda", "connect to varda"),
|
||||
huh.NewOption("Connect to Lou", "connect to lou"),
|
||||
huh.NewOption("Start RDP Connection", "start rdp connection"),
|
||||
).
|
||||
Value(&choice),
|
||||
),
|
||||
)
|
||||
|
||||
err := form.Run()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch choice {
|
||||
case "Start":
|
||||
connect()
|
||||
// startSequence()
|
||||
case "stop Work":
|
||||
runCommand("timew", "stop", "work")
|
||||
case "start break":
|
||||
runCommand("timew", "stop", "work")
|
||||
runCommand("timew", "start", "break")
|
||||
case "stop break":
|
||||
runCommand("timew", "stop", "break")
|
||||
runCommand("timew", "start", "work")
|
||||
case "show week summary":
|
||||
runCommand("timew", "summary", ":week", "work")
|
||||
case "show month summary":
|
||||
runCommand("timew", "summary", ":month", "work")
|
||||
case "wake lou":
|
||||
wakeLou()
|
||||
case "connect to varda":
|
||||
connectToVarda()
|
||||
case "connect to lou":
|
||||
connectToLou()
|
||||
case "start rdp connection":
|
||||
startRDPConnection()
|
||||
case "export":
|
||||
fmt.Println("Exporting Work times")
|
||||
}
|
||||
}
|
||||
|
||||
func connect() {
|
||||
runCommand("timew", "start", "work")
|
||||
wakeLou()
|
||||
|
|
@ -128,9 +74,9 @@ func connect() {
|
|||
}
|
||||
defer session.Close()
|
||||
|
||||
go forwardPort(sshClient, "2048", cfg.LouIP, "22")
|
||||
go forwardPort(sshClient, "2048", cfg.WorkstationIP, "22")
|
||||
|
||||
go forwardPort(sshClient, "6000", cfg.LouIP, "3389")
|
||||
go forwardPort(sshClient, "6000", cfg.WorkstationIP, "3389")
|
||||
|
||||
connectToLou()
|
||||
}
|
||||
|
|
@ -212,9 +158,9 @@ func wakeLou() {
|
|||
fmt.Sprintf("%v", cfg.SSHPort),
|
||||
cfg.SSHUser,
|
||||
cfg.SSHHost,
|
||||
cfg.VardaUser,
|
||||
cfg.VardaHost,
|
||||
cfg.LouMac)
|
||||
cfg.JumpUser,
|
||||
cfg.JumpHost,
|
||||
cfg.WorkstationMac)
|
||||
args := strings.Split(sshCommand, " ")
|
||||
log.Println(args)
|
||||
runCommand("ssh", args[1:]...)
|
||||
|
|
@ -222,7 +168,7 @@ func wakeLou() {
|
|||
|
||||
func connectToVarda() {
|
||||
sshCommand := fmt.Sprintf("ssh -tt -L 2048:%s:22 %s@%s",
|
||||
cfg.LouHost,
|
||||
cfg.WorkstationHost,
|
||||
cfg.SSHUser,
|
||||
cfg.SSHHost)
|
||||
args := strings.Split(sshCommand, " ")
|
||||
|
|
@ -231,7 +177,7 @@ func connectToVarda() {
|
|||
|
||||
func connectToLou() {
|
||||
sshCommand := fmt.Sprintf("ssh -tt -L 6000:%s:3389 -p 2048 %s@127.0.0.1",
|
||||
cfg.LouHost,
|
||||
cfg.WorkstationHost,
|
||||
cfg.SSHUser)
|
||||
args := strings.Split(sshCommand, " ")
|
||||
runCommand("ssh", args[1:]...)
|
||||
|
|
@ -243,3 +189,65 @@ func startRDPConnection() {
|
|||
cfg.RDPPassword)
|
||||
runCommand("bash", "-c", rdpCommand)
|
||||
}
|
||||
|
||||
func makeChoice() {
|
||||
var choice string
|
||||
form := huh.NewForm(
|
||||
huh.NewGroup(
|
||||
huh.NewSelect[string]().
|
||||
Title("What would you like to do?").
|
||||
Options(
|
||||
huh.NewOption("Start", "Start"),
|
||||
huh.NewOption("Stop Work", "stop Work"),
|
||||
huh.NewOption("Show Week Summary", "show week summary"),
|
||||
huh.NewOption("Show Month Summary", "show month summary"),
|
||||
huh.NewOption("Start Break", "start break"),
|
||||
huh.NewOption("Stop Break", "stop break"),
|
||||
huh.NewOption("Export Timetable", "export"),
|
||||
huh.NewOption("Wake Lou", "wake lou"),
|
||||
huh.NewOption("Connect to Varda", "connect to varda"),
|
||||
huh.NewOption("Connect to Lou", "connect to lou"),
|
||||
huh.NewOption("Start RDP Connection", "start rdp connection"),
|
||||
).
|
||||
Value(&choice),
|
||||
),
|
||||
)
|
||||
|
||||
err := form.Run()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch choice {
|
||||
case "Start":
|
||||
connect()
|
||||
// startSequence()
|
||||
case "stop Work":
|
||||
runCommand("timew", "stop", "work")
|
||||
case "start break":
|
||||
runCommand("timew", "stop", "work")
|
||||
runCommand("timew", "start", "break")
|
||||
case "stop break":
|
||||
runCommand("timew", "stop", "break")
|
||||
runCommand("timew", "start", "work")
|
||||
case "show week summary":
|
||||
runCommand("timew", "summary", ":week", "work")
|
||||
case "show month summary":
|
||||
runCommand("timew", "summary", ":month", "work")
|
||||
case "wake lou":
|
||||
wakeLou()
|
||||
case "connect to varda":
|
||||
connectToVarda()
|
||||
case "connect to lou":
|
||||
connectToLou()
|
||||
case "start rdp connection":
|
||||
startRDPConnection()
|
||||
case "export":
|
||||
fmt.Println("Exporting Work times")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
makeChoice()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue