Skip to content

Commit

Permalink
Refactor towards cmd pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
giftkugel committed Aug 24, 2024
1 parent d983a9f commit 4636e21
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 86 deletions.
6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ function prepare() {
}

function clean() {
rm -rf bin
echo "The bin/ directory was cleaned"
rm -rf bin/ dist/
echo "The bin/ and dist/ directories were cleaned"
echo
}

Expand All @@ -51,7 +51,7 @@ function build() {
FILE_NAME="$NAME.$VERSION-$OS_NAME-$GO_ARCH"
DIR="$OS_NAME-$GO_ARCH"
echo "Build $NICE_NAME version $VERSION ($GIT_HASH) for $GO_OS $GO_ARCH into $DIR"
GOOS=$GO_OS GOARCH=$GO_ARCH go build -ldflags="-s -w -X 'main.Version=$VERSION' -X 'main.GitHash=$GIT_HASH'" -o bin/$DIR/$FILE_NAME$FILE_EXTENSION main.go
GOOS=$GO_OS GOARCH=$GO_ARCH go build -ldflags="-s -w -X 'main.Version=$VERSION' -X 'main.GitHash=$GIT_HASH'" -o bin/$DIR/$FILE_NAME$FILE_EXTENSION ./cmd/stopnik

CURRENT_DIR=$(pwd)
cd bin/$DIR
Expand Down
21 changes: 0 additions & 21 deletions cmd/help.go

This file was deleted.

74 changes: 74 additions & 0 deletions cmd/stopnik/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"bufio"
"flag"
"fmt"
"github.com/webishdev/stopnik/internal/config"
"github.com/webishdev/stopnik/internal/crypto"
"github.com/webishdev/stopnik/internal/server"
logger "github.com/webishdev/stopnik/log"
"gopkg.in/yaml.v3"
"os"
"os/signal"
"syscall"
)

func printVersion(version string, gitHash string) {
fmt.Printf("STOPnik %s - %s\n", version, gitHash)
}

func printHelp(version string, gitHash string) {
fmt.Printf("STOPnik %s - %s\n\n", version, gitHash)
flag.Usage()
}

func readPassword() {
fmt.Printf("Password: ")
passwordScanner := bufio.NewScanner(os.Stdin)
passwordScanner.Scan()
password := passwordScanner.Text()
fmt.Printf("Salt: ")
saltScanner := bufio.NewScanner(os.Stdin)
saltScanner.Scan()
salt := saltScanner.Text()
result := crypto.Sha512SaltedHash(password, salt)
fmt.Printf("Hashed value is: %s\n\n", result)
}

func start(configurationFile *string) error {
configLoader := config.NewConfigLoader(os.ReadFile, yaml.Unmarshal)

currentConfig, configError := readConfiguration(configurationFile, configLoader)
if configError != nil {
return configError
}

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

stopnikServer := server.NewStopnikServer(currentConfig)

go func() {
sig := <-sigs
logger.Debug("Received signal %s", sig)
stopnikServer.Shutdown()
}()

stopnikServer.Start()

return nil
}

func readConfiguration(configurationFile *string, configLoader *config.Loader) (*config.Config, error) {
currentConfig, configError := configLoader.LoadConfig(*configurationFile)
if configError != nil {
fmt.Printf("STOPnik %s - %s\n\n", Version, GitHash)
fmt.Printf("%v", configError)
return nil, configError
}
logger.SetLogLevel(currentConfig.Server.LogLevel)
logger.Info("Config loaded from %s", *configurationFile)

return currentConfig, nil
}
33 changes: 33 additions & 0 deletions cmd/stopnik/stopnik.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"flag"
"os"
)

var Version = "development"
var GitHash = "none"

func main() {
isHelp := flag.Bool("help", false, "Show help message")
showVersion := flag.Bool("version", false, "Show version information")
askPassword := flag.Bool("password", false, "Ask for password and salt to create hash")
configurationFile := flag.String("file", "config.yml", "Configuration file to use")
flag.Parse()

if *isHelp {
printHelp(Version, GitHash)
os.Exit(0)
} else if *showVersion {
printVersion(Version, GitHash)
os.Exit(0)
} else if *askPassword {
readPassword()
os.Exit(0)
}

startError := start(configurationFile)
if startError != nil {
os.Exit(1)
}
}
62 changes: 0 additions & 62 deletions main.go

This file was deleted.

0 comments on commit 4636e21

Please sign in to comment.