Skip to content

Commit

Permalink
fix: adress linter warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Braun <rainbowstack@gmail.com>
  • Loading branch information
bluebrown committed Jan 22, 2024
1 parent a024464 commit 2dd6413
Show file tree
Hide file tree
Showing 28 changed files with 98 additions and 82 deletions.
3 changes: 1 addition & 2 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (
"os"
"os/signal"

_ "modernc.org/sqlite"

"github.com/bluebrown/kobold/config"
"github.com/bluebrown/kobold/store"
"github.com/bluebrown/kobold/store/schema"
"github.com/bluebrown/kobold/task"
_ "modernc.org/sqlite"
)

func init() {
Expand Down
6 changes: 3 additions & 3 deletions cmd/confix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ func run() error {
return fmt.Errorf("git-credentials: %w", err)
}

if err := os.WriteFile(filepath.Join(out, "kobold.toml"), buf.Bytes(), 0644); err != nil {
if err := os.WriteFile(filepath.Join(out, "kobold.toml"), buf.Bytes(), 0600); err != nil {
return fmt.Errorf("write: %w", err)
}

if len(gc) < 1 {
return nil
}

if err := os.WriteFile(filepath.Join(out, ".git-credentials"), []byte(gc), 0644); err != nil {
if err := os.WriteFile(filepath.Join(out, ".git-credentials"), []byte(gc), 0600); err != nil {
return fmt.Errorf("write: %w", err)
}

if err := os.WriteFile(filepath.Join(out, ".gitconfig"), confix.MakeGitConfig(), 0644); err != nil {
if err := os.WriteFile(filepath.Join(out, ".gitconfig"), confix.MakeGitConfig(), 0600); err != nil {
return fmt.Errorf("write: %w", err)
}

Expand Down
1 change: 1 addition & 0 deletions cmd/image-ref-updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
func main() {
p := &krm.Processor{}
cmd := command.Build(p, command.StandaloneEnabled, false)

if err := cmd.Execute(); err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "%v\n", err)
os.Exit(1)
Expand Down
12 changes: 6 additions & 6 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
Expand All @@ -10,16 +11,15 @@ import (
"os/signal"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/errgroup"
_ "modernc.org/sqlite"

"github.com/bluebrown/kobold/config"
"github.com/bluebrown/kobold/http/api"
"github.com/bluebrown/kobold/http/webhook"
"github.com/bluebrown/kobold/store"
"github.com/bluebrown/kobold/store/schema"
"github.com/bluebrown/kobold/task"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/errgroup"
_ "modernc.org/sqlite"
)

func init() {
Expand Down Expand Up @@ -101,7 +101,7 @@ func listenAndServeContext(ctx context.Context, name, addr string, handler http.
}

var (
server = http.Server{Addr: addr, Handler: handler}
server = http.Server{Addr: addr, Handler: handler, ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 10 * time.Second}
errc = make(chan error, 1)
)

Expand All @@ -117,7 +117,7 @@ func listenAndServeContext(ctx context.Context, name, addr string, handler http.
defer cancel()
return server.Shutdown(ctx)
case err := <-errc:
if err == http.ErrServerClosed {
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return fmt.Errorf("server %s: %w", name, err)
Expand Down
2 changes: 0 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* read a user friendly config file, and prepare the database accordingly. */
package config

import (
Expand Down Expand Up @@ -61,7 +60,6 @@ func (cfg *Config) Apply(ctx context.Context, q *model.Queries) error {
}

for _, c := range cfg.Channels {
// ch := model.ChannelPutParams{Name: c.Name, DecoderName: store.NullString{String: c.Decoder, Valid: c.Decoder != ""}}
ch := model.ChannelPutParams{Name: c.Name, DecoderName: null.NewString(c.Decoder, c.Decoder != "")}
if err := q.ChannelPut(ctx, ch); err != nil {
return fmt.Errorf("create channel %q: %w", c.Name, err)
Expand Down
11 changes: 8 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestReadDir(t *testing.T) {
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.givePath, func(t *testing.T) {
var got Config
err := ReadConfD(filepath.Join("testdata", tt.givePath), &got)
Expand All @@ -55,11 +56,15 @@ func TestReadDir(t *testing.T) {
b2 bytes.Buffer
)

toml.NewEncoder(&b1).Encode(got)
toml.NewEncoder(&b2).Encode(tt.wantConfig)
if err := toml.NewEncoder(&b1).Encode(got); err != nil {
t.Fatal(err)
}

t.Errorf("ReadFile() got:\n%s\n\nwant:\n%s\n", b1.String(), b2.String())
if err := toml.NewEncoder(&b2).Encode(tt.wantConfig); err != nil {
t.Fatal(err)
}

t.Errorf("ReadFile() got:\n%s\n\nwant:\n%s\n", b1.String(), b2.String())
}
})
}
Expand Down
6 changes: 5 additions & 1 deletion config/confix/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ func MakeConfig(v1 *old.NormalizedConfig) (*config.Config, error) {
}

uri := git.PackageURI{}
uri.UnmarshalText([]byte(fmt.Sprintf("%s@%s%s", repo.URL, sub.Branch, scope)))
if err := uri.UnmarshalText([]byte(fmt.Sprintf("%s@%s%s", repo.URL, sub.Branch, scope))); err != nil {
fmt.Printf("[WARN] sub=%q repo=%q scope=%q: invalid uri, skipping!\n",
sub.Name, repo.Name, scope)
continue
}

scope = strings.ReplaceAll(scope, "/", "-")
scope = strings.TrimSuffix(scope, "-")
Expand Down
2 changes: 1 addition & 1 deletion config/confix/old/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package old

// in memory representation of the config used to setup kobold
// this is used so that the user facing config can have different
// formats. I.e. v1 and v2 or a flat kubernetes configmap
// formats. I.e. v1 and v2 or a flat kubernetes configmap.
type NormalizedConfig struct {
// List of endpoints to listen on
Endpoints []EndpointSpec `json:"endpoints,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions config/confix/old/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

func TestConfig(t *testing.T) {
os.Setenv("ACR_TOKEN", "test-header")
os.Setenv("GIT_USR", "test-usr")
os.Setenv("GIT_PAT", "test-pwd")
os.Setenv("NAMESPACE", "kobold")
t.Setenv("ACR_TOKEN", "test-header")
t.Setenv("GIT_USR", "test-usr")
t.Setenv("GIT_PAT", "test-pwd")
t.Setenv("NAMESPACE", "kobold")

conf, err := ReadPath("testdata/config.yaml")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion config/confix/old/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "strings"
// cannot be inferred, an empty string is returned
//
// TODO: handle edge cases. i.e. azure repo url contains
// github.com for some reason
// github.com for some reason.
func InferGitProvider(giturl string) GitProvider {
if strings.Contains(giturl, "github.com") {
return ProviderGithub
Expand Down
2 changes: 1 addition & 1 deletion config/confix/old/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type normalizer interface {

// read the config at the given path and expand its env var references the
// config will be coded into an intermediate struct based on the version and
// afterwards normalized
// afterwards normalized.
func ReadPath(path string) (*NormalizedConfig, error) {
b, err := readExpandFile(path)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ func UseEnv(env []string, prefix string) func(*flag.Flag) {
return func(f *flag.Flag) {
name := strings.ReplaceAll(strings.ToUpper(f.Name), "-", "_")
name = prefix + name

f.Usage = fmt.Sprintf("%s (env: %s)", f.Usage, name)

if val, ok := Lookup(env, name, ""); ok {
f.Value.Set(val)
if err := f.Value.Set(val); err != nil {
panic(err)
}
}
}
}

// searches env for key and returns the value if found, otherwise fallback is
// returned. Returns a boolen indicating if the key was found
// returned. Returns a boolen indicating if the key was found.
func Lookup(env []string, key, fallback string) (string, bool) {
key += "="
for _, e := range env {
Expand Down
4 changes: 3 additions & 1 deletion config/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"log/slog"
)

func SetLog(w io.Writer, fmt string, lvl slog.Level) {
func SetLog(w io.Writer, fmt string, lvl slog.Leveler) {
ho := &slog.HandlerOptions{Level: lvl}

var handler slog.Handler
switch fmt {
case "json":
Expand All @@ -17,6 +18,7 @@ func SetLog(w io.Writer, fmt string, lvl slog.Level) {
handler = slog.NewTextHandler(w, ho)
defer slog.Warn("unknown log format", "fmt", fmt, "using", "text")
}

logger := slog.New(handler)
slog.SetDefault(logger)
}
9 changes: 3 additions & 6 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ func (o *options) Bind(fs *flag.FlagSet) *options {
}

func Options() *options {
var (
dir = os.TempDir()
err error
)
dir := os.TempDir()

if dir, err = os.UserConfigDir(); err == nil {
d := filepath.Join(dir, "kobold")
if cd, err := os.UserConfigDir(); err == nil {
d := filepath.Join(cd, "kobold")
if err = os.MkdirAll(d, 0755); err == nil {
dir = d
}
Expand Down
18 changes: 9 additions & 9 deletions git/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

// initialite the git repo at dir and set the remote origin to uri
// initialite the git repo at dir and set the remote origin to uri.
func Init(ctx context.Context, dir, uri string) error {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("mkdir %q: %w", dir, err)
Expand All @@ -25,7 +25,7 @@ func Init(ctx context.Context, dir, uri string) error {
return nil
}

// fetch the given refs from origin with depth 1
// fetch the given refs from origin with depth 1.
func FetchShallow(ctx context.Context, dir string, refs ...string) error {
refs = unique(refs)
args := []string{"fetch", "--depth", "1", "origin"}
Expand All @@ -39,7 +39,7 @@ func FetchShallow(ctx context.Context, dir string, refs ...string) error {
}

// perform init and fetch in one step but only init if the repo doesn't exist.
// otherwise, update its fetch refs and re-fetch with depth 1
// otherwise, update its fetch refs and re-fetch with depth 1.
func Ensure(ctx context.Context, dir, uri string, refs ...string) error {
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
Expand All @@ -59,39 +59,39 @@ func Ensure(ctx context.Context, dir, uri string, refs ...string) error {
}

// switch to ref. This should be called after Ensure
// to get a writeable branch
// to get a writeable branch.
func Switch(ctx context.Context, dir, ref string) error {
_, err := run(ctx, dir, "checkout", ref)
return err
}

// create a new branch and check it out
// create a new branch and check it out.
func CheckoutB(ctx context.Context, dir, ref string) error {
_, err := run(ctx, dir, "checkout", "-b", ref)
return err
}

// add all files in given dir to the index
// add all files in given dir to the index.
func AddRoot(ctx context.Context, dir string) error {
_, err := run(ctx, dir, "add", ".")
return err
}

// commit the index with the given message
// commit the index with the given message.
func Commit(ctx context.Context, dir, msg string) error {
_, err := run(ctx, dir, "commit", "-m", msg)
return err
}

// push the given refs to origin
// push the given refs to origin.
func Push(ctx context.Context, dir string, refs ...string) error {
args := []string{"push", "origin"}
args = append(args, refs...)
_, err := run(ctx, dir, args...)
return err
}

// perform add, commit, and push in one step, on the current branch
// perform add, commit, and push in one step, on the current branch.
func Publish(ctx context.Context, dir, ref, msg string) error {
if err := AddRoot(ctx, dir); err != nil {
return fmt.Errorf("git add: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions git/pkguri.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import (
"strings"
)

// the package uri is a special uri format to specifiy a git repo, ref, and
// package path withing the repo under the ref. the uri is of the form:
// the package uri is a special uri format to specify a git repo, ref, and
// package path within the repo under the ref. the uri is of the form:
//
// <repo>@<ref>[<pkg>]
//
// where <repo> is the git repo uri, <ref> is the git ref (branch, tag, commit),
// and <pkg> is the package path within the repo. if <pkg> is not specified, the
// root of the repo is assumed
// root of the repo is assumed.
type PackageURI struct {
Repo string `json:"repo,omitempty" toml:"repo"`
Ref string `json:"ref,omitempty" toml:"ref"`
Pkg string `json:"pkg,omitempty" toml:"pkg"`
}

// FIXME: appending .git can lead to confusion or invalid URIs
// FIXME: appending .git can lead to confusion or invalid URIs.
func (r *PackageURI) String() string {
return fmt.Sprintf("%s.git@%s%s", r.Repo, r.Ref, r.Pkg)
}
Expand Down
Loading

0 comments on commit 2dd6413

Please sign in to comment.