Commit c6811a9
Changed files (6)
cmd
cmd/generate/generate_passphrase.go
@@ -2,7 +2,7 @@ package generate
import (
"fmt"
- "log"
+ "log/slog"
"strings"
"github.com/kahnwong/swissknife/cmd/utils"
@@ -11,16 +11,16 @@ import (
"github.com/spf13/cobra"
)
-func generatePassphrase() string {
+func generatePassphrase() (string, error) {
// Generate 6 words using the diceware algorithm.
list, err := diceware.Generate(6)
if err != nil {
- log.Fatal(err)
+ return "", err
}
res := strings.Join(list, "-")
- return res
+ return res, nil
}
var generatePassphraseCmd = &cobra.Command{
@@ -29,7 +29,10 @@ var generatePassphraseCmd = &cobra.Command{
Long: `Generate passphrase. Result is copied to clipboard.`,
Run: func(cmd *cobra.Command, args []string) {
// main
- passphrase := generatePassphrase()
+ passphrase, err := generatePassphrase()
+ if err != nil {
+ slog.Error("Error generating passphrase")
+ }
utils.CopyToClipboard(passphrase)
fmt.Printf("%s\n", passphrase)
},
cmd/generate/generate_password.go
@@ -2,7 +2,7 @@ package generate
import (
"fmt"
- "log"
+ "log/slog"
"github.com/sethvargo/go-password/password"
@@ -10,15 +10,15 @@ import (
"github.com/spf13/cobra"
)
-func generatePassword() string {
+func generatePassword() (string, error) {
// Generate a password that is 64 characters long with 10 digits, 10 symbols,
// allowing upper and lower case letters, disallowing repeat characters.
res, err := password.Generate(32, 10, 0, false, false)
if err != nil {
- log.Fatal(err)
+ return "", err
}
- return res
+ return res, nil
}
var generatePasswordCmd = &cobra.Command{
@@ -27,7 +27,10 @@ var generatePasswordCmd = &cobra.Command{
Long: `Generate password. Result is copied to clipboard.`,
Run: func(cmd *cobra.Command, args []string) {
// main
- password := generatePassword()
+ password, err := generatePassword()
+ if err != nil {
+ slog.Error("Error generating password")
+ }
utils.CopyToClipboard(password)
fmt.Printf("%s\n", password)
},
cmd/generate/generate_ssh_key.go
@@ -47,16 +47,16 @@ func returnKeyPath(fileName string) string {
}
// main
-func generateSSHKeyEDSA(fileName string) {
+func generateSSHKeyEDSA(fileName string) error {
// Generate a new Ed25519 private key
//// If rand is nil, crypto/rand.Reader will be used
public, private, err := ed25519.GenerateKey(nil)
if err != nil {
- panic(err)
+ return err
}
p, err := ssh.MarshalPrivateKey(crypto.PrivateKey(private), "")
if err != nil {
- panic(err)
+ return err
}
// private key
@@ -68,10 +68,12 @@ func generateSSHKeyEDSA(fileName string) {
// public key
publicKey, err := ssh.NewPublicKey(public)
if err != nil {
- panic(err)
+ return err
}
publicKeyString := "ssh-ed25519" + " " + base64.StdEncoding.EncodeToString(publicKey.Marshal())
writeStringToFile(fmt.Sprintf("%s.pub", fileName), publicKeyString, 0644)
+
+ return nil
}
var generateSSHKeyCmd = &cobra.Command{
@@ -86,7 +88,10 @@ var generateSSHKeyCmd = &cobra.Command{
}
// main
- generateSSHKeyEDSA(args[0])
+ err := generateSSHKeyEDSA(args[0])
+ if err != nil {
+ fmt.Print(err)
+ }
fmt.Printf("SSH key created at: %s\n", returnKeyPath(args[0]))
},
}
cmd/get/get_iface.go
@@ -11,22 +11,22 @@ import (
// unix: `netstat -nr -f inet`
// get iface-owned app: `ifconfig -v utun3 | grep "agent domain"`
-func getIface() string {
+func getIface() (string, error) {
r, err := netroute.New()
if err != nil {
- panic(err)
+ return "", err
}
iface, _, _, err := r.Route(
net.IPv4(104, 16, 133, 229), // cloudflare
)
if err != nil {
- panic(err)
+ return "", err
}
//fmt.Printf("%v, %v, %v, %v\n", iface, gw, src, err)
- return iface.Name
+ return iface.Name, nil
}
var getIfaceCmd = &cobra.Command{
@@ -34,7 +34,11 @@ var getIfaceCmd = &cobra.Command{
Short: "Get iface",
Long: `Get iface used for public internet access`,
Run: func(cmd *cobra.Command, args []string) {
- fmt.Println(getIface())
+ iface, err := getIface()
+ if err != nil {
+ fmt.Println(err)
+ }
+ fmt.Println(iface)
},
}
cmd/get/get_ip.go
@@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io"
- "log"
"net"
"net/http"
"strings"
@@ -12,10 +11,10 @@ import (
"github.com/spf13/cobra"
)
-func getLocalIP() string {
+func getLocalIP() (string, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
- log.Fatal(err)
+ return "", err
}
localAddr := conn.LocalAddr().(*net.UDPAddr)
@@ -30,19 +29,19 @@ func getLocalIP() string {
fmt.Println("Invalid address format")
}
- return fmt.Sprintf("%v", localIP)
+ return fmt.Sprintf("%v", localIP), nil
}
-func getPublicIP() string {
+func getPublicIP() (string, error) {
// make request
resp, err := http.Get("https://httpbin.org/ip")
if err != nil {
- log.Fatal(err)
+ return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
- log.Fatal(err)
+ return "", err
}
// parse
@@ -53,10 +52,10 @@ func getPublicIP() string {
var jsonResponse Response
err = json.Unmarshal(body, &jsonResponse)
if err != nil {
- log.Fatal(err)
+ return "", err
}
- return jsonResponse.Origin
+ return jsonResponse.Origin, nil
}
var getIPCmd = &cobra.Command{
@@ -64,8 +63,17 @@ var getIPCmd = &cobra.Command{
Short: "Get IP information",
Long: `Get IP information`,
Run: func(cmd *cobra.Command, args []string) {
- fmt.Printf("Local IP : %s\n", getLocalIP())
- fmt.Printf("Public IP : %s\n", getPublicIP())
+ localIP, err := getLocalIP()
+ if err != nil {
+ fmt.Println(err)
+ }
+ fmt.Printf("Local IP : %s\n", localIP)
+
+ publicIP, err := getPublicIP()
+ if err != nil {
+ fmt.Println(err)
+ }
+ fmt.Printf("Public IP : %s\n", publicIP)
},
}
cmd/misc/shouldideploytoday.go
@@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
- "log"
+ "log/slog"
"net/http"
"time"
@@ -19,34 +19,37 @@ type ShouldIDeploy struct {
Message string `json:"message"`
}
-func ShouldIDeployToday() ShouldIDeploy {
+func ShouldIDeployToday() (ShouldIDeploy, error) {
url := "https://shouldideploy.today/api?tz=Asia%2FBangkok"
method := "GET"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
+ if err != nil {
+ return ShouldIDeploy{}, err
+ }
if err != nil {
- fmt.Println(err)
+ return ShouldIDeploy{}, err
}
res, err := client.Do(req)
if err != nil {
- fmt.Println(err)
+ return ShouldIDeploy{}, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
- fmt.Println(err)
+ return ShouldIDeploy{}, err
}
// decode
var response ShouldIDeploy
if err := json.Unmarshal(body, &response); err != nil {
- log.Println("Can not unmarshal JSON")
+ slog.Error("Can not unmarshal JSON")
}
- return response
+ return response, nil
}
var Cmd = &cobra.Command{
@@ -54,7 +57,10 @@ var Cmd = &cobra.Command{
Short: "Should I deploy today?",
Long: `Should I deploy today?`,
Run: func(cmd *cobra.Command, args []string) {
- response := ShouldIDeployToday()
+ response, err := ShouldIDeployToday()
+ if err != nil {
+ fmt.Println(err)
+ }
// init output colors
green := color.New(color.FgGreen).SprintFunc()