Skip to content

Commit

Permalink
feat: submodule parsing parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Jul 18, 2024
1 parent a4c9f20 commit 1ed0370
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 24 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Cacao

Candy Server with WebUI

## Run

```bash
# loglevel=[info] listen=[:80] storage=[.]
cacao

# loglevel=[debug] listen=[127.0.0.1:8080] storage=[/var/lib/cacao]
cacao --loglevel=debug --listen=127.0.0.1:8080 --stroage=/var/lib/cacao
```
18 changes: 18 additions & 0 deletions argp/argp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package argp

import (
"os"
"strings"
)

func Get(name string, value string) string {
for _, arg := range os.Args {
option := strings.SplitN(arg, "=", 2)
if len(option) == 2 {
if "--"+name == option[0] {
return option[1]
}
}
}
return value
}
18 changes: 18 additions & 0 deletions argp/argp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package argp

import (
"os"
"testing"
)

func TestGetString(t *testing.T) {
want := "default"
if got := Get("key", "default"); got != want {
t.Fatalf(`GetString("key", "default") = %v, want %v`, got, want)
}
os.Args = append(os.Args, "--key=value")
want = "value"
if got := Get("key", "default"); got != want {
t.Fatalf(`GetString("key", "default") = %v, want %v`, got, want)
}
}
41 changes: 41 additions & 0 deletions configs/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package configs

import (
"github.com/lanthora/cacao/logger"
"github.com/lanthora/cacao/storage"
"gorm.io/gorm"
)

type Config struct {
gorm.Model
Key string `gorm:"uniqueIndex"`
Value string
}

func init() {
db := storage.Get()

if err := db.AutoMigrate(Config{}); err != nil {
logger.Fatal("auto migrate configs failed: %v", err)
}
}

func SetString(key string, value string) {
db := storage.Get()
config := &Config{Key: key}
db.Take(config)
config.Value = value
db.Save(config)
}

func GetString(key string) (value string, ok bool) {
db := storage.Get()
config := &Config{Key: key}

if result := db.Where(config).Take(config); result.Error == nil {
value = config.Value
ok = true
}

return
}
15 changes: 15 additions & 0 deletions configs/configs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package configs

import (
"testing"
)

func TestSetGetString(t *testing.T) {
if got, ok := GetString("key"); ok {
t.Fatalf(`GetString("key"") = %v, %v, want %v %v`, got, ok, "", false)
}
SetString("key", "value")
if got, ok := GetString("key"); !ok || got != "value" {
t.Fatalf(`GetString("key"") = %v, %v, want %v %v`, got, ok, "value", true)
}
}
21 changes: 20 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,23 @@ go 1.22.5

require github.com/sirupsen/logrus v1.9.3

require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/text v0.14.0 // indirect
modernc.org/libc v1.22.5 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect
modernc.org/sqlite v1.23.1 // indirect
)

require (
github.com/glebarez/sqlite v1.11.0
golang.org/x/sys v0.7.0 // indirect
gorm.io/gorm v1.25.11
)
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY=
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM=
modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk=
11 changes: 4 additions & 7 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package logger
import (
"bytes"
"fmt"
"os"

"github.com/lanthora/cacao/argp"
"github.com/sirupsen/logrus"
)

Expand All @@ -24,18 +24,15 @@ func init() {
logger = logrus.New()
logger.SetReportCaller(true)
logger.SetFormatter(&logFormatter{})
if value := os.Getenv("CUCURBITA_LOGLEVEL"); len(value) != 0 {
setLogLevel(value)
}
}

func setLogLevel(level string) {
switch level {
switch argp.Get("loglevel", "info") {
case "debug":
logger.SetLevel(logrus.DebugLevel)
case "info":
logger.SetLevel(logrus.InfoLevel)
}

Info("loglevel=[%v]", logger.GetLevel().String())
}

func Fatal(format string, args ...interface{}) {
Expand Down
19 changes: 3 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
package main

import (
"flag"

"github.com/lanthora/cacao/argp"
"github.com/lanthora/cacao/logger"
)

var (
storageDir string
listenAddr string
logLevel string
)

func init() {
flag.StringVar(&listenAddr, "listen", ":80", "set listen address")
flag.StringVar(&logLevel, "log", "info", "set log level")
flag.StringVar(&storageDir, "storage", ".", "set storage directory")
flag.Parse()
}

func main() {
logger.Info("listen=[%v] log=[%v] storage=[%v]", listenAddr, logLevel, storageDir)
listen := argp.Get("listen", ":80")
logger.Info("listen=[%v]", listen)
}
34 changes: 34 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package storage

import (
"os"
"path"

"github.com/glebarez/sqlite"
"github.com/lanthora/cacao/argp"
"github.com/lanthora/cacao/logger"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
)

var db *gorm.DB

func init() {
storageDir := argp.Get("storage", ".")
err := os.MkdirAll(storageDir, os.ModeDir|os.ModePerm)
if err != nil {
logger.Fatal("make storage dir failed: %v", err)
}
db, err = gorm.Open(sqlite.Open(path.Join(storageDir, "sqlite.db")), &gorm.Config{
Logger: gormlogger.Default.LogMode(gormlogger.Silent),
})
if err != nil {
logger.Fatal("open storage database failed: %v", err)
}

logger.Info("storage=[%v]", storageDir)
}

func Get() *gorm.DB {
return db
}

0 comments on commit 1ed0370

Please sign in to comment.