From ae10f2fbe1ad7ff37a3403b6d49a486011840381 Mon Sep 17 00:00:00 2001 From: ygelfand Date: Wed, 21 Feb 2024 00:25:15 -0500 Subject: [PATCH] build improvements Signed-off-by: ygelfand --- .github/workflows/ci.yaml | 13 +++++++++++-- Dockerfile | 3 ++- cmd/debug.go | 2 +- cmd/proxy.go | 8 +++++++- cmd/root.go | 12 +++++++++--- internal/api/app.go | 3 ++- internal/api/debug.go | 1 + 7 files changed, 33 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bbf81cf..f612ae9 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -3,8 +3,9 @@ name: ci on: push: branches: - - "main" - + - 'main' + tags: + - 'v*' jobs: docker: runs-on: ubuntu-latest @@ -25,10 +26,18 @@ jobs: uses: docker/metadata-action@v5 with: images: ygelfand/go-powerwall + tags: | + type=ref,event=branch + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=sha,prefix={{branch}}- + type=raw,value=latest,enable={{is_default_branch}} - name: Build and push uses: docker/build-push-action@v5 with: context: . + build-args: | + "BUILD_VERSION=${{ steps.meta.outputs.version }}" platforms: linux/amd64,linux/arm64 push: true tags: ${{ steps.meta.outputs.tags }} diff --git a/Dockerfile b/Dockerfile index 651af87..c59cc51 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,8 @@ RUN go mod download COPY main.go main.go COPY internal/ internal/ COPY cmd/ cmd/ -RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o go-powerwall main.go +ARG BUILD_VERSION +RUN CGO_ENABLED=0 go build -ldflags="-w -s -X github.com/ygelfand/go-powerwall/cmd.debugMode=false -X github.com/ygelfand/go-powerwall/cmd.GoPowerwallVersion=${BUILD_VERSION}" -o go-powerwall main.go FROM scratch COPY --from=builder /workspace/go-powerwall /go-powerwall diff --git a/cmd/debug.go b/cmd/debug.go index 492156e..d758ba9 100644 --- a/cmd/debug.go +++ b/cmd/debug.go @@ -19,7 +19,7 @@ func newDebugCmd(opts *powerwallOptions) *cobra.Command { pwr := powerwall.NewPowerwallGateway(opts.endpoint, opts.password) debug := pwr.RunQuery("DeviceControllerQuery", nil) var prettyJSON bytes.Buffer - debug = pwr.RunQuery("ComponentsQuery", nil) + //debug = pwr.RunQuery("ComponentsQuery", nil) err := json.Indent(&prettyJSON, []byte(*debug), "", "\t") if err != nil { fmt.Println("JSON parse error: ", err) diff --git a/cmd/proxy.go b/cmd/proxy.go index 414201b..4500721 100644 --- a/cmd/proxy.go +++ b/cmd/proxy.go @@ -3,6 +3,7 @@ package cmd import ( "time" + "github.com/gin-gonic/gin" "github.com/spf13/cobra" "github.com/ygelfand/go-powerwall/internal/api" "github.com/ygelfand/go-powerwall/internal/powerwall" @@ -12,6 +13,7 @@ type proxyOptions struct { *powerwallOptions refreshInterval uint32 onDemand bool + listenOn string } func newProxyCmd(opts *powerwallOptions) *cobra.Command { @@ -22,14 +24,18 @@ func newProxyCmd(opts *powerwallOptions) *cobra.Command { Long: `Start powerwall proxy server`, Run: func(cmd *cobra.Command, args []string) { pwr := powerwall.NewPowerwallGateway(o.endpoint, o.password) + if !o.debugMode { + gin.SetMode(gin.ReleaseMode) + } app := api.NewApi(pwr, o.onDemand) if !o.onDemand { go pwr.PeriodicRefresh(time.Duration(o.refreshInterval) * time.Second) } - app.Run(":8080") + app.Run(o.listenOn) }, } proxyCmd.Flags().BoolVarP(&o.onDemand, "ondemand", "o", false, "disable periodic refresh") + proxyCmd.Flags().StringVarP(&o.listenOn, "listen", "l", ":8080", "host:port to listen on") proxyCmd.Flags().Uint32VarP(&o.refreshInterval, "refresh", "r", 30, "periodic refresh frequency in seconds") return proxyCmd } diff --git a/cmd/root.go b/cmd/root.go index ea9e07f..06c34e7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,15 +1,20 @@ package cmd import ( + "log" "os" "github.com/spf13/cobra" "github.com/spf13/viper" ) +// set at build time +var debugMode = "true" + type powerwallOptions struct { - endpoint string - password string + endpoint string + password string + debugMode bool } var rootCmd = &cobra.Command{ @@ -26,11 +31,12 @@ func Execute() { } func init() { + log.Println(debugMode) viper.SetEnvPrefix("POWERWALL") viper.SetDefault("ENDPOINT", "https://192.168.91.1/tedapi") viper.BindEnv("PASSWORD") viper.BindEnv("ENDPOINT") - o := &powerwallOptions{} + o := &powerwallOptions{debugMode: debugMode == "true"} rootCmd.PersistentFlags().StringVarP(&o.endpoint, "endpoint", "e", viper.GetString("ENDPOINT"), "powerwall endpoint url") rootCmd.PersistentFlags().StringVarP(&o.password, "password", "p", viper.GetString("PASSWORD"), "powerwall installer password") rootCmd.MarkPersistentFlagRequired("password") diff --git a/internal/api/app.go b/internal/api/app.go index d2d1103..cddf5db 100644 --- a/internal/api/app.go +++ b/internal/api/app.go @@ -17,8 +17,9 @@ func NewApi(p *powerwall.PowerwallGateway, forceRefresh bool) *Api { } func (api *Api) Run(listen string) { - + gin.LoggerWithWriter router := gin.Default() + router.SetTrustedProxies(nil) base := router.Group("/api") { v1 := base.Group("/v1") diff --git a/internal/api/debug.go b/internal/api/debug.go index 0c99246..b49ff6e 100644 --- a/internal/api/debug.go +++ b/internal/api/debug.go @@ -8,6 +8,7 @@ func (app *Api) debugConfig(c *gin.Context) { if app.forceRefresh || c.Query("refresh") == "true" { app.powerwall.UpdateConfig() } + c c.JSON(200, app.powerwall.Config) }