Skip to content

Commit

Permalink
add new watch flag for up command
Browse files Browse the repository at this point in the history
Signed-off-by: Nikhil Sharma <nikhilsharma230303@gmail.com>
  • Loading branch information
NikhilSharmaWe committed May 3, 2023
1 parent 0b1e92b commit 419204b
Show file tree
Hide file tree
Showing 21 changed files with 3,232 additions and 594 deletions.
86 changes: 76 additions & 10 deletions commands/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@ package commands

import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var (
skipPush bool
skipDeploy bool
watch bool
)

func init() {

upFlagset := pflag.NewFlagSet("up", pflag.ExitOnError)
upFlagset.BoolVar(&skipPush, "skip-push", false, "Skip pushing function to remote registry")
upFlagset.BoolVar(&skipDeploy, "skip-deploy", false, "Skip function deployment")
upFlagset.BoolVar(&watch, "watch", false, "Watch for changes in files and re-deploy")
upCmd.Flags().AddFlagSet(upFlagset)

build, _, _ := faasCmd.Find([]string{"build"})
Expand Down Expand Up @@ -64,20 +70,80 @@ func preRunUp(cmd *cobra.Command, args []string) error {
}

func upHandler(cmd *cobra.Command, args []string) error {
if err := runBuild(cmd, args); err != nil {
return err
}
fmt.Println()
if !skipPush {
if err := runPush(cmd, args); err != nil {
if !watch {
if err := runBuild(cmd, args); err != nil {
return err
}
fmt.Println()
}
if !skipDeploy {
if err := runDeploy(cmd, args); err != nil {

if !skipPush {
if err := runPush(cmd, args); err != nil {
return err
}
}

if !skipDeploy {
if err := runDeploy(cmd, args); err != nil {
return err
}
}
} else {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()

err = filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return watcher.Add(path)
}
return nil
})

if err != nil {
return err
}

debounceTimer := time.NewTimer(0 * time.Second)
defer debounceTimer.Stop()

for {
select {
case event, ok := <-watcher.Events:
if !ok {
return fmt.Errorf("watcher's Events channel is closed")
}

if event.Op == fsnotify.Write {
debounceTimer.Reset(5 * time.Second)
}

case err, ok := <-watcher.Errors:
if !ok {
return fmt.Errorf("watcher's Errors channel is closed")
}
return err

case <-debounceTimer.C:
if err := runBuild(cmd, args); err != nil {
return err
}

if !skipPush {
if err := runPush(cmd, args); err != nil {
return err
}
}

if err := runDeploy(cmd, args); err != nil {
return err
}
}
}
}

return nil
}
24 changes: 1 addition & 23 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,34 @@ require (
)

require (
github.com/fsnotify/fsnotify v1.6.0
github.com/google/go-containerregistry v0.13.0
github.com/mitchellh/go-wordwrap v1.0.1
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cheggaaa/pb/v3 v3.1.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.12.1 // indirect
github.com/docker/cli v20.10.20+incompatible // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect
github.com/nats-io/nats.go v1.22.1 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/nats-io/stan.go v0.10.4 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/openfaas/nats-queue-worker v0.0.0-20230117214128-3615ccb286cc // indirect
github.com/otiai10/copy v1.9.0 // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/sethvargo/go-password v0.2.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/vbatts/tar-split v0.11.2 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 419204b

Please sign in to comment.