Skip to content

Commit

Permalink
remove fmtr status from init(), include container runtime info in ver…
Browse files Browse the repository at this point in the history
…sion cmd
  • Loading branch information
verdverm committed Jul 30, 2023
1 parent 1c82ebe commit 00c8546
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 50 deletions.
14 changes: 13 additions & 1 deletion cmd/hof/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/spf13/cobra"

"github.com/hofstadter-io/hof/cmd/hof/ga"

"github.com/hofstadter-io/hof/cmd/hof/verinfo"
"github.com/hofstadter-io/hof/lib/container"
)

const versionMessage = `hof - the high code framework
Expand All @@ -23,6 +23,7 @@ CueVersion: %s
OS / Arch: %s %s
ConfigDir: %s
CacheDir: %s
Containers: %s
Author: Hofstadter, Inc
Homepage: https://hofstadter.io
Expand All @@ -48,6 +49,16 @@ var VersionCmd = &cobra.Command{
configDir, _ := os.UserConfigDir()
cacheDir, _ := os.UserCacheDir()

err := container.InitClient()
if err != nil {
fmt.Println(err)
}

rt, err := container.GetVersion()
if err != nil {
fmt.Println(err)
}

fmt.Printf(
versionMessage,
verinfo.Version,
Expand All @@ -59,6 +70,7 @@ var VersionCmd = &cobra.Command{
verinfo.BuildArch,
filepath.Join(configDir,"hof"),
filepath.Join(cacheDir,"hof"),
rt,
)
},
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/hof/verinfo/verinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ var (
BuildOS = "Unknown"
BuildArch = "Unknown"

CueVersion = "0.5.0"
// todo, look this up from deps
CueVersion = "0.6.0-beta.1"

// this is a version we can fetch with hof mod
// the value gets injected into templates in various places
// the default here is set to something useful for dev
// the release version is the same as the cli running it
HofVersion = "v0.6.8-beta.10"
HofVersion = "v0.6.8-rc.5"
)


func init() {
info, _ := debug.ReadBuildInfo()
GoVersion = info.GoVersion
Expand Down
28 changes: 8 additions & 20 deletions lib/container/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ type Image struct {
Names []string
}

var portExp = regexp.MustCompile(`:(\d+)->`)

type Container struct {
Ports PortList
Image string
Expand All @@ -28,6 +26,14 @@ type Container struct {

type PortList []int

type NameList []string

type structuredPort struct {
HostPort int `json:"host_port"`
}

var portExp = regexp.MustCompile(`:(\d+)->`)

func (l *PortList) UnmarshalJSON(b []byte) error {
if len(b) == 0 {
return nil
Expand Down Expand Up @@ -72,8 +78,6 @@ func (l *PortList) UnmarshalJSON(b []byte) error {
return nil
}

type NameList []string

func (l *NameList) UnmarshalJSON(b []byte) error {
if len(b) == 0 {
return nil
Expand All @@ -98,19 +102,3 @@ func (l *NameList) UnmarshalJSON(b []byte) error {

return nil
}

type structuredPort struct {
HostPort int `json:"host_port"`
}

type RuntimeVersion struct {
Client struct {
Version string
APIVersion string
}
Server struct {
Version string
APIVersion string
MinAPIVersion string
}
}
10 changes: 0 additions & 10 deletions lib/container/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,6 @@ func (r runtime) execJSON(ctx context.Context, resp any, args ...string) error {
return nil
}

func (r runtime) Version(ctx context.Context) (RuntimeVersion, error) {
var rv RuntimeVersion
if err := r.execJSON(ctx, &rv, "version", "--format", "{{ json . }}"); err != nil {
return rv, fmt.Errorf("exec json: %w", err)
}

return rv, nil
}

func (r runtime) Containers(ctx context.Context, name Name) ([]Container, error) {
args := []string{
"container", "ls", "-a",
Expand Down Expand Up @@ -272,6 +263,5 @@ func ndjson[T any](r io.Reader) ([]T, error) {
}
}

// fmt.Println("ts:", ts)
return ts, nil
}
33 changes: 33 additions & 0 deletions lib/container/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,45 @@ package container

import (
"context"
"fmt"
"time"
)

type RuntimeVersion struct {
Name string
Client struct {
Version string
APIVersion string
}
Server struct {
Version string
APIVersion string
MinAPIVersion string
}
}

func GetVersion() (RuntimeVersion, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

return rt.Version(ctx)
}

func (r runtime) Version(ctx context.Context) (RuntimeVersion, error) {
var rv RuntimeVersion
if err := r.execJSON(ctx, &rv, "version", "--format", "{{ json . }}"); err != nil {
return rv, fmt.Errorf("exec json: %w", err)
}

rv.Name = string(r.bin)
return rv, nil
}

func (r RuntimeVersion) String() string {
return fmt.Sprintf(
"%s [%s (client) | %s (server)]",
r.Name,
r.Client.Version,
r.Server.Version,
)
}
10 changes: 10 additions & 0 deletions lib/fmt/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func (fmtr *Formatter) Call(filename string, content []byte, config any) ([]byte
}

func (fmtr *Formatter) WaitForRunning(retry int, delay time.Duration) error {

// We should probably rethink how this works, such that
// we minimize our exec out to docker (et al)
// we can exec once and then check on all formatters

// fmt.Println("wait-running.0:", fmtr.Name, fmtr.Status, fmtr.Running, fmtr.Ready)
// return if already running
if fmtr.Running {
Expand All @@ -99,6 +104,11 @@ func (fmtr *Formatter) WaitForRunning(retry int, delay time.Duration) error {

if fmtr.Status == "running" {
fmtr.Running = true
// we need to be more detailed here if we are going to make this a sync.Once
// (well we can't once here, but we want to generally for most other uses)
// here, we update all formatter statuses in the wait/retry loop of each
// this might end up being ok, since the next formatter will come in return immediately
// ... maybe we can just remove this call?
err = updateFormatterStatus()
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions lib/fmt/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"strings"
"sort"
"time"

"github.com/olekukonko/tablewriter"
Expand Down Expand Up @@ -440,6 +441,8 @@ func Info(which string) (err error) {
}
}

sort.Strings(fmtr.Available)

if fmtr.Container != nil {
rows = append(rows, []string{
fmtr.Name,
Expand All @@ -466,8 +469,6 @@ func Info(which string) (err error) {
return rows, nil
},
)

return nil
}

func updateFormatterStatus() error {
Expand Down
16 changes: 1 addition & 15 deletions lib/fmt/fmtrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func init() {
}

// gracefully init images / containers
err := Init()
err := container.InitClient()
if err != nil {
if debug {
fmt.Println("fmt init error:", err)
Expand All @@ -93,20 +93,6 @@ func init() {
}
}

func Init() error {
err := container.InitClient()
if err != nil {
return err
}

err = updateFormatterStatus()
if err != nil {
return err
}

return nil
}

type Formatter struct {
// name, same as tools/%
Name string
Expand Down

0 comments on commit 00c8546

Please sign in to comment.