Skip to content

Commit

Permalink
refactor: separate print and gather
Browse files Browse the repository at this point in the history
  • Loading branch information
jon4hz committed Jul 24, 2022
1 parent 7ac9abf commit 4103b01
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 32 deletions.
6 changes: 3 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package config

type Config struct {
Hostname HostnameConfig
Hostname Hostname
}

type HostnameConfig struct {
type Hostname struct {
Disable bool
Color string
Figlet bool
FigletFont string
FigletFontDir string
FigletColor string
}
24 changes: 21 additions & 3 deletions internal/context/context.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package context

import "github.com/jon4hz/gmotd/internal/config"
import (
"time"

"github.com/jon4hz/gmotd/internal/config"
)

type Context struct {
Config *config.Config
Runtime *Runtime
Config *config.Config
Runtime *Runtime
Hostname *Hostname
Uptime *Uptime
Sysinfo *Sysinfo
}

type Runtime struct {
Expand All @@ -17,3 +24,14 @@ func New() *Context {
Runtime: &Runtime{},
}
}

type Hostname struct {
Hostname string
}
type Uptime struct {
Uptime time.Duration
}

type Sysinfo struct {
Uptime time.Duration
}
32 changes: 16 additions & 16 deletions internal/pipe/hostname/hostname.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hostname

import (
"fmt"
"math/rand"
"os"
"strings"
Expand All @@ -20,40 +21,39 @@ func (Pipe) String() string { return "hostname" }

func (Pipe) Default(ctx *context.Context) {
ctx.Config.Hostname.Figlet = true
ctx.Config.Hostname.FigletColor = "rainbow"
ctx.Config.Hostname.Color = "rainbow"
ctx.Config.Hostname.FigletFont = "standard"
ctx.Config.Hostname.FigletFontDir = "/usr/share/figlet/fonts"
}

func (Pipe) Skip(c *context.Context) bool {
return false
func (Pipe) Gather(c *context.Context) error {
h, err := os.Hostname()
if err != nil {
return fmt.Errorf("failed to get hostname: %w", err)
}
c.Hostname = &context.Hostname{
Hostname: h,
}
return nil
}

//go:embed fonts/standard.flf
var defaultFont []byte

func (Pipe) Message(c *context.Context) string {
hostname, err := os.Hostname()
if err != nil {
return ""
}
func (Pipe) Print(c *context.Context) string {
if !c.Config.Hostname.Figlet {
return hostname
return c.Hostname.Hostname
}

var f *figletlib.Font
if c.Config.Hostname.FigletFontDir != "" {
f, err := figletlib.GetFontByName(c.Config.Hostname.FigletFontDir, c.Config.Hostname.FigletFont)
if err != nil {
f, err = figletlib.ReadFontFromBytes(defaultFont)
if err != nil {
return ""
}
}

f, err = figletlib.GetFontByName(c.Config.Hostname.FigletFontDir, c.Config.Hostname.FigletFont)
if err != nil {
return ""
}
renderStr := figletlib.SprintMsg(hostname, f, c.Runtime.Width, f.Settings(), "left")
renderStr := figletlib.SprintMsg(c.Hostname.Hostname, f, c.Runtime.Width, f.Settings(), "left")

colors := func() string {
colors := colorGrid(lipgloss.Width(renderStr), lipgloss.Height(renderStr))
Expand Down
19 changes: 14 additions & 5 deletions internal/pipe/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@ type Pipe struct{}

func (Pipe) String() string { return "sysinfo" }

func (Pipe) Message(c *context.Context) string {
var s strings.Builder

func (Pipe) Gather(c *context.Context) error {
t, err := host.BootTime()
if err != nil {
return fmt.Errorf("failed to get uptime: %w", err)
}

c.Sysinfo = &context.Sysinfo{
Uptime: time.Since(time.Unix(int64(t), 0)),
}
return nil
}

func (Pipe) Print(c *context.Context) string {
if c.Sysinfo == nil {
return ""
}
uptime := time.Since(time.Unix(int64(t), 0)).Round(time.Second)
s.WriteString(fmt.Sprintf("Uptime: %s\n", uptime))
var s strings.Builder
s.WriteString(fmt.Sprintf("Uptime: %s\n", c.Uptime.Uptime))

platform, family, version, err := host.PlatformInformation()
if err != nil {
Expand Down
16 changes: 13 additions & 3 deletions internal/pipe/uptime/uptime.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uptime

import (
"fmt"
"time"

"github.com/hako/durafmt"
Expand All @@ -12,11 +13,20 @@ type Pipe struct{}

func (Pipe) String() string { return "uptime" }

func (Pipe) Message(c *context.Context) string {
func (Pipe) Gather(c *context.Context) error {
t, err := host.BootTime()
if err != nil {
return fmt.Errorf("failed to get uptime: %w", err)
}
c.Uptime = &context.Uptime{
Uptime: time.Since(time.Unix(int64(t), 0)),
}
return nil
}

func (Pipe) Print(c *context.Context) string {
if c.Uptime == nil {
return ""
}
uptime := time.Since(time.Unix(int64(t), 0)).Round(time.Second)
return "Uptime: " + durafmt.Parse(uptime).LimitFirstN(2).String()
return "Uptime: " + durafmt.Parse(c.Uptime.Uptime).LimitFirstN(2).String()
}
3 changes: 2 additions & 1 deletion internal/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

type Pipe interface {
fmt.Stringer
Message(*context.Context) string
Gather(*context.Context) error
Print(*context.Context) string
}

var Pipeline = []Pipe{
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ func main() {

var messages []string
for _, pipe := range pipeline.Pipeline {
messages = append(messages, pipe.Message(ctx))
if err := pipe.Gather(ctx); err != nil {
log.Println(err)
continue
}
messages = append(messages, pipe.Print(ctx))
}

fmt.Println(lipgloss.JoinVertical(lipgloss.Left, messages...))
Expand Down

0 comments on commit 4103b01

Please sign in to comment.