From c9e906bceeed26127163077caede01ee8280e036 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 23 Sep 2024 20:31:06 -0400 Subject: [PATCH] Initial commit --- .gitignore | 4 ++ .gitmodules | 3 + Makefile | 38 +++++++++++ cmd/bottinagenda/main.go | 5 ++ cmd/bottinagenda/root.go | 55 ++++++++++++++++ cmd/bottinagenda/server.go | 12 ++++ .../bottinagenda-acceptance/compose.yaml | 41 ++++++++++++ deployments/docker/bottinagenda/Dockerfile | 28 +++++++++ go.mod | 29 +++++++++ go.sum | 62 ++++++++++++++++++ pkg/bottinagenda/build.go | 6 ++ pkg/bottinagenda/config.go | 63 +++++++++++++++++++ pkg/bottinagenda/server_test.go | 19 ++++++ 13 files changed, 365 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 Makefile create mode 100644 cmd/bottinagenda/main.go create mode 100644 cmd/bottinagenda/root.go create mode 100644 cmd/bottinagenda/server.go create mode 100644 deployments/compose/bottinagenda-acceptance/compose.yaml create mode 100644 deployments/docker/bottinagenda/Dockerfile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 pkg/bottinagenda/build.go create mode 100644 pkg/bottinagenda/config.go create mode 100644 pkg/bottinagenda/server_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..324e667 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.env +*.env +external/ +vendor/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..0b5f275 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "external/bottin"] + path = external/bottin + url = https://git.agecem.com/bottin/bottin diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8750c23 --- /dev/null +++ b/Makefile @@ -0,0 +1,38 @@ +## This Makefile uses the help target explained in the following blogpost: +## +## https://victoria.dev/blog/how-to-create-a-self-documenting-makefile/ + +.DEFAULT_GOAL := help + +.PHONY: help +help: ## Show this help + @egrep -h '\s##\s' $(MAKEFILE_LIST) | \ + sort | \ + awk \ + 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' + +.PHONY: install +install: ## Use `go install` to build and link the executable + go install -a ./cmd/bottinagenda/ + +.PHONY: compose-ps +compose-ps: compose-acceptance-ps ## Show state of containers of all pipelines + +.PHONY: compose-acceptance-up +compose-acceptance-up: ## Build and run test containers using compose, then run the acceptance tests within + docker-compose -f deployments/compose/bottinagenda-acceptance/compose.yaml up -d --build + +.PHONY: compose-acceptance-down +compose-acceptance-down: ## Remove the containers created by acceptance pipeline + docker-compose -f deployments/compose/bottinagenda-acceptance/compose.yaml down + +.PHONY: compose-acceptance-ps +compose-acceptance-ps: ## Show state of containers from acceptance pipeline + docker-compose -f deployments/compose/bottinagenda-acceptance/compose.yaml ps + +.PHONY: compose-acceptance-logs +compose-acceptance-logs: ## Follow the logs for acceptance container + docker-compose -f deployments/compose/bottinagenda-acceptance/compose.yaml logs -f bottinagenda_api + +.PHONY: compose-acceptance-test +compose-acceptance-test: compose-acceptance-up compose-acceptance-logs ## Start acceptance pipeline and follow `go test` logs on stdout diff --git a/cmd/bottinagenda/main.go b/cmd/bottinagenda/main.go new file mode 100644 index 0000000..736ef31 --- /dev/null +++ b/cmd/bottinagenda/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + Execute() +} diff --git a/cmd/bottinagenda/root.go b/cmd/bottinagenda/root.go new file mode 100644 index 0000000..9a72e42 --- /dev/null +++ b/cmd/bottinagenda/root.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "os" + "strings" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var cfgFile string + +var rootCmd = &cobra.Command{ + Use: "bottinagenda", + Short: "Application de gestion de distribution d'agendas", +} + +// Adds child commands to the root command and sets flags +func Execute() { + if err := rootCmd.Execute(); err != nil { + os.Exit(1) + } +} + +func initConfig() { + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + // Find home directory. + home, err := os.UserHomeDir() + cobra.CheckErr(err) + + // Search config in home directory with name ".bottin-agenda" (without extension). + viper.AddConfigPath(home) + viper.SetConfigType("yaml") + viper.SetConfigName(".bottinagenda") + } + + viper.SetEnvPrefix("BOTTINAGENDA") + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viper.AutomaticEnv() // read in environment variables that match + + // If a config file is found, read it in. + if err := viper.ReadInConfig(); err == nil { + fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) + } +} + +func init() { + cobra.OnInitialize(initConfig) + + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bottinagenda.yaml)") +} diff --git a/cmd/bottinagenda/server.go b/cmd/bottinagenda/server.go new file mode 100644 index 0000000..0554fe2 --- /dev/null +++ b/cmd/bottinagenda/server.go @@ -0,0 +1,12 @@ +package main + +//serverCmd +//serverAPICmd +//serverUICmd +//init: +// - rootCmd < serverCmd +// - serverCmd < serverAPICmd +// - serverCmd < serverUICmd +//bindServerFlags (if any) on serverCmd +//bindServerAPIFlags on serverAPICmd +//bindServerUIFlags on serverUICmd diff --git a/deployments/compose/bottinagenda-acceptance/compose.yaml b/deployments/compose/bottinagenda-acceptance/compose.yaml new file mode 100644 index 0000000..6856b35 --- /dev/null +++ b/deployments/compose/bottinagenda-acceptance/compose.yaml @@ -0,0 +1,41 @@ +services: + bottin_db: + image: 'postgres:16' + environment: + POSTGRES_USER: 'bottin' + POSTGRES_PASSWORD: 'bottin' + + bottin_api: + depends_on: + - 'bottin_db' + image: 'git.agecem.com/bottin/bottin:acceptance' + build: + context: '../../../external/bottin/' + dockerfile: './deployments/docker/bottin/Dockerfile' + environment: + BOTTIN_SERVER_DB_USER: 'bottin' + BOTTIN_SERVER_DB_PASSWORD: 'bottin' + BOTTIN_SERVER_DB_HOST: 'bottin_db' + restart: 'unless-stopped' + command: ['cat', '/msg'] + + bottinagenda_db: + image: 'postgres:16' + environment: + POSTGRES_USER: 'bottinagenda' + POSTGRES_PASSWORD: 'bottinagenda' + + bottinagenda_api: + depends_on: + - 'bottinagenda_db' + image: 'git.agecem.com/bottin/bottinagenda:acceptance' + build: + context: '../../../' + dockerfile: './deployments/docker/bottinagenda/Dockerfile' + environment: + BOTTINAGENDA_SERVER_DB_USER: 'bottinagenda' + BOTTINAGENDA_SERVER_DB_PASSWORD: 'bottinagenda' + BOTTINAGENDA_SERVER_DB_HOST: 'bottinagenda_db' + #BOTTINAGENDA_BOTTIN_HOST? + restart: 'unless-stopped' + command: ['cat', '/msg'] diff --git a/deployments/docker/bottinagenda/Dockerfile b/deployments/docker/bottinagenda/Dockerfile new file mode 100644 index 0000000..420ace1 --- /dev/null +++ b/deployments/docker/bottinagenda/Dockerfile @@ -0,0 +1,28 @@ +FROM golang:1.23 as build + +ARG bottinagenda_version + +LABEL author="vlbeaudoin" + +WORKDIR /go/src/app + +COPY go.mod go.sum LICENSE ./ + +ADD cmd/ cmd/ +ADD pkg/ pkg/ +ADD queries/ queries/ +ADD ui/ ui/ + +RUN CGO_ENABLED=0 go build \ + -a \ + -o bottinagenda \ + -ldflags="-X 'git.agecem.com/bottin/bottinagenda/pkg/bottinagenda.version=$bottinagenda_version'" \ + ./cmd/bottinagenda/ + +# Alpine + +FROM alpine:3.20 as run + +COPY --from=build /go/src/app/bottinagenda /usr/bin/bottinagenda + +CMD ["bottinagenda", "help"] diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9a6d973 --- /dev/null +++ b/go.mod @@ -0,0 +1,29 @@ +module git.agecem.com/bottin/bottinagenda + +go 1.23.1 + +require ( + git.agecem.com/bottin/bottin v0.2.0 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/cobra v1.8.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/viper v1.19.0 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9ddfe3b --- /dev/null +++ b/go.sum @@ -0,0 +1,62 @@ +git.agecem.com/bottin/bottin v0.2.0 h1:zzRsk8u5fwNIak9hZxp//uLyLjhEPZGikIQLsK9x9gc= +git.agecem.com/bottin/bottin v0.2.0/go.mod h1:XMf3meOkXc93sE8tgGDRaUqOGcRWy6rtwB+O8eIxvGE= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +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/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +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/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/bottinagenda/build.go b/pkg/bottinagenda/build.go new file mode 100644 index 0000000..5b85fad --- /dev/null +++ b/pkg/bottinagenda/build.go @@ -0,0 +1,6 @@ +package bottinagenda + +// version is set at build time +var version string + +func Version() string { return version } diff --git a/pkg/bottinagenda/config.go b/pkg/bottinagenda/config.go new file mode 100644 index 0000000..7c1dca1 --- /dev/null +++ b/pkg/bottinagenda/config.go @@ -0,0 +1,63 @@ +package bottinagenda + +import "github.com/spf13/viper" + +func ParseConfig() (cfg Config, err error) { + return cfg, viper.Unmarshal(&cfg) +} + +type APIClientConfig struct { + Host string + Key string + Port int + TLS TLSClientConfig +} + +type APIServerConfig struct { + Host string + Key string + Port int + Postgres PostgresConfig + TLS TLSServerConfig +} + +type ClientConfig struct { + API APIClientConfig + OutputStyle string +} + +type Config struct { + Client ClientConfig + Server ServerConfig +} + +type PostgresConfig struct { + Database string + Host string + Password string + Port int + User string + SSLMode string +} + +type ServerConfig struct { + API APIServerConfig + UI struct { + Host string + Password string + Port int + Username string + API APIClientConfig + } +} + +type TLSClientConfig struct { + Enabled bool + SkipVerify bool +} + +type TLSServerConfig struct { + Enabled bool + CertFile string + KeyFile string +} diff --git a/pkg/bottinagenda/server_test.go b/pkg/bottinagenda/server_test.go new file mode 100644 index 0000000..62b0dea --- /dev/null +++ b/pkg/bottinagenda/server_test.go @@ -0,0 +1,19 @@ +package bottinagenda + +import ( + "testing" + + "git.agecem.com/bottin/bottin/pkg/bottin" +) + +func TestBottinAPIClient(t *testing.T) { + var bottinClient bottin.APIClient + t.Run("message", func(t *testing.T) { + t.Log(bottinClient.Message()) + }) +} + +func TestServer(t *testing.T) { + //go RunAPIServer + +}