Skip to content

Commit

Permalink
build improvements
Browse files Browse the repository at this point in the history
Signed-off-by: ygelfand <[email protected]>
  • Loading branch information
ygelfand committed Feb 21, 2024
1 parent 409ea46 commit ae10f2f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 9 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ name: ci
on:
push:
branches:
- "main"

- 'main'
tags:
- 'v*'
jobs:
docker:
runs-on: ubuntu-latest
Expand All @@ -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 }}
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion cmd/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -12,6 +13,7 @@ type proxyOptions struct {
*powerwallOptions
refreshInterval uint32
onDemand bool
listenOn string
}

func newProxyCmd(opts *powerwallOptions) *cobra.Command {
Expand All @@ -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
}
12 changes: 9 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion internal/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions internal/api/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit ae10f2f

Please sign in to comment.