Skip to content

Commit

Permalink
feat: implement bldr update --dry command
Browse files Browse the repository at this point in the history
Only for GitHub tags and releases for now.

Refs siderolabs#48.

Signed-off-by: Alexey Palazhchenko <[email protected]>
  • Loading branch information
AlekSi committed Jul 12, 2021
1 parent 533e360 commit 520002c
Show file tree
Hide file tree
Showing 16 changed files with 2,019 additions and 149 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
out
/out
/bldr
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# syntax = docker/dockerfile-upstream:1.1.2-experimental
# syntax = docker/dockerfile-upstream:1.2.0

FROM golang:1.14-alpine AS base
FROM golang:1.16-alpine AS base
ENV GO111MODULE on
ENV GOPROXY https://proxy.golang.org
ENV CGO_ENABLED 0
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const defaultPlatform = (runtime.GOOS + "/" + runtime.GOARCH)

var (
pkgRoot string
debug bool
options = &environment.Options{
BuildPlatform: environment.LinuxAmd64,
TargetPlatform: environment.LinuxAmd64,
Expand Down Expand Up @@ -48,7 +49,7 @@ func Execute() {
}

func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "", false, "Enable debug logging")
rootCmd.PersistentFlags().StringVarP(&pkgRoot, "root", "", ".", "The path to a pkg root")

options.BuildPlatform.Set(defaultPlatform) //nolint: errcheck
Expand Down
137 changes: 137 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package cmd

import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"sort"
"sync"
"text/tabwriter"
"time"

"github.com/spf13/cobra"
"github.com/talos-systems/bldr/internal/pkg/solver"
"github.com/talos-systems/bldr/internal/pkg/update"
)

type pkgInfo struct {
file string
source string
}

type updateInfo struct {
file string
*update.LatestInfo
}

var updateCmdFlag struct {
all bool
dry bool
}

// updateCmd represents the `update` command.
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update pkgs",
Run: func(cmd *cobra.Command, args []string) {
if !updateCmdFlag.dry {
log.Fatal("Real update is not implemented yet; pass `--dry` flag.")
}

loader := solver.FilesystemPackageLoader{
Root: pkgRoot,
Context: options.GetVariables(),
}

packages, err := solver.NewPackages(&loader)
if err != nil {
log.Fatal(err)
}

l := log.New(log.Writer(), "[update] ", log.Flags())
if !debug {
l.SetOutput(ioutil.Discard)
}

concurrency := runtime.GOMAXPROCS(-1)
var wg sync.WaitGroup
sources := make(chan *pkgInfo)
updates := make(chan *updateInfo)
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
defer wg.Done()

for src := range sources {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
res, err := update.Latest(ctx, src.source)
cancel()
if err != nil {
l.Print(err)
continue
}

updates <- &updateInfo{
file: src.file,
LatestInfo: res,
}
}
}()
}

var res []updateInfo
done := make(chan struct{})
go func() {
for update := range updates {
res = append(res, *update)
}
close(done)
}()

for _, node := range packages.ToSet() {
for _, step := range node.Pkg.Steps {
for _, src := range step.Sources {
sources <- &pkgInfo{
file: node.Pkg.FileName,
source: src.URL,
}
}
}
}
close(sources)
wg.Wait()
close(updates)
<-done

sort.Slice(res, func(i, j int) bool { return res[i].file < res[j].file })

w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintf(w, "%s\t%s\t%s\n", "File", "Update", "URL")

for _, info := range res {
if updateCmdFlag.all || info.HasUpdate {
url := info.LatestURL
if url == "" {
url = info.BaseURL
}

fmt.Fprintf(w, "%s\t%t\t%s\n", info.file, info.HasUpdate, url)
}
}

w.Flush()
},
}

func init() {
updateCmd.Flags().BoolVarP(&updateCmdFlag.all, "all", "a", false, "List all packages, not only updated")
updateCmd.Flags().BoolVar(&updateCmdFlag.dry, "dry", false, "Dry run: check for updates, but not actually update pkgs")
rootCmd.AddCommand(updateCmd)
}
35 changes: 15 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
module github.com/talos-systems/bldr

go 1.14
go 1.16

require (
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/alessio/shellescape v1.2.2
github.com/containerd/containerd v1.4.0-0
github.com/emicklei/dot v0.11.0
github.com/hashicorp/go-multierror v1.1.0
github.com/huandu/xstrings v1.3.1 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/moby/buildkit v0.7.1
github.com/Masterminds/semver v1.5.0
github.com/Masterminds/sprig/v3 v3.2.2
github.com/alessio/shellescape v1.4.1
github.com/containerd/containerd v1.5.2
github.com/emicklei/dot v0.16.0
github.com/google/go-github/v35 v35.3.0
github.com/hashicorp/go-multierror v1.1.1
github.com/moby/buildkit v0.8.3
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.0.1
github.com/otiai10/copy v1.2.0
github.com/spf13/cobra v1.0.0
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
gopkg.in/yaml.v2 v2.3.0
)

replace (
github.com/containerd/containerd => github.com/containerd/containerd v1.3.1-0.20200512144102-f13ba8f2f2fd
github.com/docker/docker => github.com/docker/docker v17.12.0-ce-rc1.0.20200310163718-4634ce647cf2+incompatible
github.com/otiai10/copy v1.6.0
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit 520002c

Please sign in to comment.