]> 91.132.146.200 Git - selfpaste.git/commitdiff
new client written in go
authorBanana <mail@bananas-playground.net>
Sun, 27 Feb 2022 12:21:15 +0000 (13:21 +0100)
committerBanana <mail@bananas-playground.net>
Sun, 27 Feb 2022 12:21:15 +0000 (13:21 +0100)
client/go-client/README [new file with mode: 0644]
client/go-client/go.mod [new file with mode: 0644]
client/go-client/go.sum [new file with mode: 0644]
client/go-client/selfpaste.go [new file with mode: 0644]

diff --git a/client/go-client/README b/client/go-client/README
new file mode 100644 (file)
index 0000000..6e356be
--- /dev/null
@@ -0,0 +1,8 @@
+This is a client written in go to be used with selfpaste
+https://://www.bananas-playground.net/projekt/selfpaste
+
+!WARNING!
+This is a very simple, with limited experience written, go program.
+Use at own risk and feel free to improve.
+
+Howto build:
diff --git a/client/go-client/go.mod b/client/go-client/go.mod
new file mode 100644 (file)
index 0000000..b7aa292
--- /dev/null
@@ -0,0 +1,3 @@
+module selfpaste/go-client
+
+go 1.17
diff --git a/client/go-client/go.sum b/client/go-client/go.sum
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/client/go-client/selfpaste.go b/client/go-client/selfpaste.go
new file mode 100644 (file)
index 0000000..46b69f2
--- /dev/null
@@ -0,0 +1,77 @@
+package main
+
+import (
+       "errors"
+       "fmt"
+       "log"
+       "math/rand"
+       "os"
+)
+
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
+// You should have received a copy of the
+// COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+// along with this program.  If not, see http://www.sun.com/cddl/cddl.html
+
+// 2019 - 2022 https://://www.bananas-playground.net/projekt/selfpaste
+
+const website = "https://www.bananas-playground.net/projekt/selfpaste"
+const version = "1.0"
+// used for non-existing default config
+const letters = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
+
+func main() {
+       loadConfig()
+}
+
+/**
+ * Check and display error with additional message
+ */
+func errorCheck(e error, msg string) {
+       if e != nil {
+               log.Fatal(msg,e)
+       }
+}
+
+/**
+ * load or even create a default config
+ * $HOME/.selfpaste.yaml
+ */
+func loadConfig() {
+       homeDir, err := os.UserHomeDir()
+       errorCheck(err, "No $HOME directory available?")
+       log.Printf("Your $HOME: %s \n", homeDir)
+
+       var configFile = homeDir + "/.selfpaste.yaml"
+
+       if _, err := os.Stat(configFile); errors.Is(err, os.ErrNotExist) {
+               log.Printf("Config file not found. Creating default: %s \n", configFile)
+
+               newConfig, err := os.Create(configFile)
+               errorCheck(err, "Can not create config file!")
+               defer newConfig.Close()
+
+
+               _, err = fmt.Fprintf(newConfig, "# selfpaste go client config file.\n")
+               errorCheck(err, "Can not write to new config file")
+               fmt.Fprintf(newConfig, "# See %s for more details.\n", website)
+               fmt.Fprintf(newConfig, "# Version: %s\n", version)
+               fmt.Fprintf(newConfig, "endpoint:\n")
+               fmt.Fprintf(newConfig, "  host: http://your-seflpaste-endpoi.nt\n")
+               fmt.Fprintf(newConfig, "  secret: %s\n", randStringBytes(50))
+
+               log.Fatalf("New default config file created: - %s - Edit and launch again!",configFile)
+       }
+}
+
+/**
+ * just a random string
+ */
+func randStringBytes(n int) string {
+       b := make([]byte, n)
+       for i := range b {
+               b[i] = letters[rand.Intn(len(letters))]
+       }
+       return string(b)
+}