1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-22 08:35:57 +01:00

Configuration file and networking options.

This commit is contained in:
Juliusz Chroboczek 2024-10-23 18:34:15 +02:00
parent e33029072b
commit 8d3302506a

View file

@ -3,13 +3,16 @@ package main
import ( import (
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"crypto/tls"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"log" "log"
"net/http"
"os" "os"
"path/filepath"
"slices" "slices"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@ -18,6 +21,18 @@ import (
"github.com/jech/galene/group" "github.com/jech/galene/group"
) )
type configuration struct {
Server string `json:"server"`
AdminUsername string `json:"admin-username"`
AdminPassword string `json:"admin-password"`
AdminToken string `json:"admin-token"`
}
var insecure bool
var serverURL, adminUsername, adminPassword, adminToken string
var client http.Client
type command struct { type command struct {
command func(string, []string) command func(string, []string)
description string description string
@ -31,6 +46,15 @@ var commands = map[string]command{
} }
func main() { func main() {
configdir, err := os.UserConfigDir()
if err != nil {
log.Fatalf("UserConfigDir: %v", err)
}
configFile := filepath.Join(
filepath.Join(configdir, "galene"),
"galenectl.json",
)
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintf( fmt.Fprintf(
flag.CommandLine.Output(), flag.CommandLine.Output(),
@ -55,6 +79,18 @@ func main() {
os.Args[0], os.Args[0],
) )
} }
flag.StringVar(&serverURL, "server", "",
"server `url`")
flag.BoolVar(&insecure, "insecure", false,
"don't check server certificates")
flag.StringVar(&configFile, "config", configFile,
"configuration `file`")
flag.StringVar(&adminUsername, "admin-username", "",
"administrator `username`")
flag.StringVar(&adminPassword, "admin-password", "",
"administrator `password`")
flag.StringVar(&adminToken, "admin-token",
"", "administrator `token`")
flag.Parse() flag.Parse()
if flag.NArg() < 1 { if flag.NArg() < 1 {
@ -62,6 +98,33 @@ func main() {
os.Exit(1) os.Exit(1)
} }
config, err := readConfig(configFile)
if err != nil {
log.Fatalf("Failed to read configuration file: %v", err)
}
if serverURL == "" {
serverURL = config.Server
}
if serverURL == "" {
serverURL = "https://localhost:8443"
}
if adminUsername == "" {
adminUsername = config.AdminUsername
}
if adminPassword == "" {
adminPassword = config.AdminPassword
}
if adminToken == "" {
adminToken = config.AdminToken
}
if insecure {
t := http.DefaultTransport.(*http.Transport).Clone()
t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client.Transport = t
}
cmdname := flag.Args()[0] cmdname := flag.Args()[0]
command, ok := commands[cmdname] command, ok := commands[cmdname]
if !ok { if !ok {
@ -71,6 +134,25 @@ func main() {
command.command(cmdname, flag.Args()[1:]) command.command(cmdname, flag.Args()[1:])
} }
func readConfig(filename string) (configuration, error) {
var config configuration
f, err := os.Open(filename)
if err != nil {
if os.IsNotExist(err) {
return config, nil
}
return config, err
}
defer f.Close()
decoder := json.NewDecoder(f)
decoder.DisallowUnknownFields()
err = decoder.Decode(&config)
if err != nil {
return config, err
}
return config, nil
}
func makePassword(pw string, algorithm string, iterations, length, saltlen, cost int) (group.Password, error) { func makePassword(pw string, algorithm string, iterations, length, saltlen, cost int) (group.Password, error) {
salt := make([]byte, saltlen) salt := make([]byte, saltlen)
_, err := rand.Read(salt) _, err := rand.Read(salt)