-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
144 lines (118 loc) · 4.38 KB
/
root.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
package cmd
import (
"database/sql"
"fmt"
"log"
"os"
"github.com/TwiN/go-color"
"github.com/spf13/cobra"
_ "github.com/mattn/go-sqlite3" // Driver for sql
"github.com/linde12/gowol"
"github.com/spf13/viper"
)
var cfgFile string
var dbPath string
var idNum string
var macAddress string
var ipAddress string
var alias string
var homeDir string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Version: "1.6.5",
Use: "wakie",
Short: "Wake-on-LAN utility",
Long: `Utility for sending Magic Packets for Wake-on-LAN, as well as managing an address book of computer MAC Addresses.`,
Run: func(cmd *cobra.Command, args []string) {
switch {
case idNum != "":
macAddress, ipAddress = queryMAC("ID", idNum)
case alias != "":
macAddress, ipAddress = queryMAC("Alias", alias)
case macAddress != "":
// Nothing to do here since MAC address is passed in directly. Still need a case for it
// so that default case isn't executed, which is for no flags being passed in.
default:
fmt.Println(color.Ize(color.Bold, "Welcome to Wakie. Please specify a MAC Address or --help flag for list of commands."))
os.Exit(1)
}
sendPacket(macAddress, ipAddress)
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}
func init() {
fmt.Println(color.Ize(color.Bold, "Wakie:"))
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Config file (default is $HOME/.config/wakie/wakie.yaml or app folder)")
rootCmd.PersistentFlags().StringVarP(&idNum, "id", "i", "", "ID of saved MAC address")
rootCmd.PersistentFlags().StringVarP(&macAddress, "mac", "m", "", "Manually entered MAC address")
rootCmd.PersistentFlags().StringVar(&ipAddress, "ip", "255.255.255.255", "IP address of MAC address. Needed in case host is connected to multiple networks.")
rootCmd.PersistentFlags().StringVarP(&alias, "alias", "a", "", "Alias of saved MAC address")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
var err error
homeDir, err = os.UserHomeDir()
if err != nil {
log.Fatalf("Unable to get users home dir: %s", err)
}
configPath := homeDir + "/.config/wakie"
// Search config in home directory with name ".wakie" (without extension).
viper.AddConfigPath(configPath)
viper.AddConfigPath(".")
viper.SetConfigName("wakie")
viper.SetConfigType("yaml")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, " - Using config file:", viper.ConfigFileUsed())
}
dbPath = viper.GetString("db.dbPath")
}
// queryMAC looks up MAC address by ID or Alias and returns MAC and IP address as a string
func queryMAC(flagName, flagValue string) (string, string) {
fmt.Printf(" - Getting MAC Address with %s of %s\n", flagName, flagValue)
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Fatalf("Error opening database file. %s", err)
}
var idColumn string
var macColumn string
var ipColumn string
var aliasColumn string
querySQLStmt := fmt.Sprintf("SELECT * FROM computers WHERE `%s` = '%s';", flagName, flagValue)
queryResult := db.QueryRow(querySQLStmt)
err = queryResult.Scan(&idColumn, &macColumn, &ipColumn, &aliasColumn)
if err != nil {
log.Fatalf("Unable to find MAC with %s of %s", flagName, flagValue)
}
return macColumn, ipColumn
}
// sendPacket sends the magic packet to the specified address
func sendPacket(targetMacAddress, targetIPAddress string) {
packet, err := gowol.NewMagicPacket(targetMacAddress)
if err != nil {
log.Fatalln(err)
}
err = packet.Send(targetIPAddress)
if err != nil {
log.Fatal("Error sending magic packet")
}
fmt.Printf(" - Magic Packet sent to %s\n", targetMacAddress)
}