Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minified #128

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 2 additions & 40 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,3 @@
package cmd

import (
_ "embed"
"fmt"

"github.com/koki-develop/clive/internal/util"
"github.com/spf13/cobra"
)

package cmd;import(_ "embed";"fmt";"github.com/koki-develop/clive/internal/util";"github.com/spf13/cobra");
//go:embed clive.yml
var configInitTemplate []byte

var initCmd = &cobra.Command{
Use: "init",
Short: "Create a config file",
Long: "Create a config file.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
exists, err := util.Exists(flagConfig)
if err != nil {
return err
}
if exists {
return fmt.Errorf("%s already exists", flagConfig)
}

f, err := util.CreateFile(flagConfig)
if err != nil {
return err
}
defer f.Close()

if _, err := f.Write(configInitTemplate); err != nil {
return err
}

_, _ = fmt.Printf("Created %s\n", flagConfig)
return nil
},
}
var configInitTemplate []byte;var initCmd=&cobra.Command{Use:"init",Short:"Create a config file",Long:"Create a config file.",Args:cobra.ExactArgs(0),RunE:func(cmd *cobra.Command,args []string)error{exists,err:=util.Exists(flagConfig);if err!=nil{return err};if exists{return fmt.Errorf("%s already exists",flagConfig)};f,err:=util.CreateFile(flagConfig);if err!=nil{return err};defer f.Close();if _,err:=f.Write(configInitTemplate);err!=nil{return err};_,_=fmt.Printf("Created %s\n",flagConfig);return nil}};
57 changes: 1 addition & 56 deletions cmd/notify.go
Original file line number Diff line number Diff line change
@@ -1,56 +1 @@
package cmd

import (
"context"
"fmt"
"io"
"time"

"github.com/charmbracelet/lipgloss"
"github.com/google/go-github/v55/github"
"github.com/koki-develop/clive/internal/cache"

"github.com/koki-develop/clive/internal/styles"
"github.com/koki-develop/clive/internal/util"
)

func notifyNewRelease(w io.Writer) error {
s, err := cache.NewStore(12 * time.Hour)
if err != nil {
return err
}

c, err := s.Get("release")
if err != nil {
return err
}

var release github.RepositoryRelease
if c != nil && !c.Expired() {
// If the cache exists and has not expired, no notification is given.
return nil
} else {
// Retrieve the latest release and save cache.
cl := github.NewClient(nil)
r, _, err := cl.Repositories.GetLatestRelease(context.Background(), "koki-develop", "clive")
if err != nil {
return err
}
release = *r
if err := s.Set("release", map[string]string{"name": *release.Name}); err != nil {
return err
}
}

// If a newer version is released, notify it.
if util.Version(*release.Name).Newer(util.Version(version)) {
txt := styles.StyleNotificationText.Render(fmt.Sprintf("A new version (%s) is available!", *release.Name))
link := fmt.Sprintf("See: %s", styles.StyleLink.Render(fmt.Sprintf("https://github.com/koki-develop/clive/releases/%s", *release.Name)))
n := lipgloss.JoinVertical(lipgloss.Center, txt, link)
n = util.Border(n, styles.StyleNotificationBorder)

_, _ = fmt.Fprintln(w, n)
}

return nil
}
package cmd;import("context";"fmt";"io";"time";"github.com/charmbracelet/lipgloss";"github.com/google/go-github/v55/github";"github.com/koki-develop/clive/internal/cache";"github.com/koki-develop/clive/internal/styles";"github.com/koki-develop/clive/internal/util");func notifyNewRelease(w io.Writer)error{s,err:=cache.NewStore(12*time.Hour);if err!=nil{return err};c,err:=s.Get("release");if err!=nil{return err};var release github.RepositoryRelease;if c!=nil&&!c.Expired(){return nil}else{cl:=github.NewClient(nil);r,_,err:=cl.Repositories.GetLatestRelease(context.Background(),"koki-develop","clive");if err!=nil{return err};release=*r;if err:=s.Set("release",map[string]string{"name":*release.Name});err!=nil{return err}};if util.Version(*release.Name).Newer(util.Version(version)){txt:=styles.StyleNotificationText.Render(fmt.Sprintf("A new version (%s) is available!",*release.Name));link:=fmt.Sprintf("See: %s",styles.StyleLink.Render(fmt.Sprintf("https://github.com/koki-develop/clive/releases/%s",*release.Name)));n:=lipgloss.JoinVertical(lipgloss.Center,txt,link);n=util.Border(n,styles.StyleNotificationBorder);_,_=fmt.Fprintln(w,n)};return nil};
65 changes: 1 addition & 64 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,64 +1 @@
package cmd

import (
"os"
"runtime/debug"

"github.com/spf13/cobra"
)

var (
version string

flagConfig string
)

var rootCmd = &cobra.Command{
Use: "clive",
Short: "Automates terminal operations",
Long: "Automates terminal operations.",
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
/*
* version
*/

if version == "" {
if info, ok := debug.ReadBuildInfo(); ok {
version = info.Main.Version
}
}
rootCmd.Version = version
_ = notifyNewRelease(os.Stderr)

/*
* commands
*/

rootCmd.AddCommand(
initCmd,
startCmd,
validateCmd,
)

/*
* flags
*/

for _, cmd := range []*cobra.Command{
startCmd,
initCmd,
validateCmd,
} {
// --config
cmd.Flags().StringVarP(&flagConfig, "config", "c", "./clive.yml", "config file name")
}
}
package cmd;import("os";"runtime/debug";"github.com/spf13/cobra");var(version string;flagConfig string);var rootCmd=&cobra.Command{Use:"clive",Short:"Automates terminal operations",Long:"Automates terminal operations."};func Execute(){err:=rootCmd.Execute();if err!=nil{os.Exit(1)}};func init(){if version==""{if info,ok:=debug.ReadBuildInfo();ok{version=info.Main.Version}};rootCmd.Version=version;_=notifyNewRelease(os.Stderr);rootCmd.AddCommand(initCmd,startCmd,validateCmd);for _,cmd:=range []*cobra.Command{startCmd,initCmd,validateCmd}{cmd.Flags().StringVarP(&flagConfig,"config","c","./clive.yml","config file name")}};
30 changes: 1 addition & 29 deletions cmd/start.go
Original file line number Diff line number Diff line change
@@ -1,29 +1 @@
package cmd

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/koki-develop/clive/internal/ui"
"github.com/spf13/cobra"
)

var startCmd = &cobra.Command{
Use: "start",
Short: "Start cLive actions",
Long: "Start cLive actions.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
m := ui.New(flagConfig)
defer m.Close()

p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {
return err
}

if err := m.Err(); err != nil {
return err
}

return nil
},
}
package cmd;import(tea "github.com/charmbracelet/bubbletea";"github.com/koki-develop/clive/internal/ui";"github.com/spf13/cobra");var startCmd=&cobra.Command{Use:"start",Short:"Start cLive actions",Long:"Start cLive actions.",Args:cobra.ExactArgs(0),RunE:func(cmd *cobra.Command,args []string)error{m:=ui.New(flagConfig);defer m.Close();p:=tea.NewProgram(m);if _,err:=p.Run();err!=nil{return err};if err:=m.Err();err!=nil{return err};return nil}};
24 changes: 1 addition & 23 deletions cmd/validate.go
Original file line number Diff line number Diff line change
@@ -1,23 +1 @@
package cmd

import (
"fmt"

"github.com/koki-develop/clive/internal/config"
"github.com/spf13/cobra"
)

var validateCmd = &cobra.Command{
Use: "validate",
Short: "Validate a config file",
Long: "Validate a config file.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := config.Load(flagConfig); err != nil {
return err
}

_, _ = fmt.Printf("Config file %s is valid!\n", flagConfig)
return nil
},
}
package cmd;import("fmt";"github.com/koki-develop/clive/internal/config";"github.com/spf13/cobra");var validateCmd=&cobra.Command{Use:"validate",Short:"Validate a config file",Long:"Validate a config file.",Args:cobra.ExactArgs(0),RunE:func(cmd *cobra.Command,args []string)error{if _,err:=config.Load(flagConfig);err!=nil{return err};_,_=fmt.Printf("Config file %s is valid!\n",flagConfig);return nil}};
56 changes: 1 addition & 55 deletions internal/browser/browser.go
Original file line number Diff line number Diff line change
@@ -1,55 +1 @@
package browser

import (
"time"

"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
)

type BrowserConfig struct {
Bin *string
URL string
Headless bool
}

func Open(cfg *BrowserConfig) (*rod.Page, error) {
b, err := launchBrowser(cfg)
if err != nil {
return nil, err
}

p, err := b.Page(proto.TargetCreateTarget{URL: cfg.URL})
if err != nil {
return nil, err
}
if err := p.WaitIdle(time.Minute); err != nil {
return nil, err
}

return p, nil
}

func launchBrowser(cfg *BrowserConfig) (*rod.Browser, error) {
path, _ := launcher.LookPath()
if cfg.Bin != nil {
path = *cfg.Bin
}

u, err := launcher.New().
Leakless(true).
Headless(cfg.Headless).
Bin(path).
Launch()
if err != nil {
return nil, err
}

b := rod.New().ControlURL(u).NoDefaultDevice()
if err := b.Connect(); err != nil {
return nil, err
}

return b, nil
}
package browser;import("time";"github.com/go-rod/rod";"github.com/go-rod/rod/lib/launcher";"github.com/go-rod/rod/lib/proto");type BrowserConfig struct{Bin *string;URL string;Headless bool};func Open(cfg *BrowserConfig)(*rod.Page,error){b,err:=launchBrowser(cfg);if err!=nil{return nil,err};p,err:=b.Page(proto.TargetCreateTarget{URL:cfg.URL});if err!=nil{return nil,err};if err:=p.WaitIdle(time.Minute);err!=nil{return nil,err};return p,nil};func launchBrowser(cfg *BrowserConfig)(*rod.Browser,error){path,_:=launcher.LookPath();if cfg.Bin!=nil{path=*cfg.Bin};u,err:=launcher.New().Leakless(true).Headless(cfg.Headless).Bin(path).Launch();if err!=nil{return nil,err};b:=rod.New().ControlURL(u).NoDefaultDevice();if err:=b.Connect();err!=nil{return nil,err};return b,nil};
46 changes: 1 addition & 45 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,45 +1 @@
package cache

import (
"encoding/json"
"io"
"time"

"github.com/mitchellh/mapstructure"
)

type Cache struct {
Expiration time.Time `json:"expiration"`
Data interface{} `json:"data"`
}

func New(ttl time.Duration, data interface{}) *Cache {
return &Cache{
Expiration: time.Now().Add(ttl),
Data: data,
}
}

func (c *Cache) Expired() bool {
return time.Now().After(c.Expiration)
}

func (c *Cache) Bind(dst interface{}) error {
if err := mapstructure.Decode(c.Data, dst); err != nil {
return err
}
return nil
}

func (c *Cache) Write(w io.Writer) error {
buf, err := json.Marshal(c)
if err != nil {
return err
}

if _, err := w.Write(buf); err != nil {
return err
}

return nil
}
package cache;import("encoding/json";"io";"time";"github.com/mitchellh/mapstructure");type Cache struct{Expiration time.Time `json:"expiration"`;Data interface{} `json:"data"`};func New(ttl time.Duration,data interface{})*Cache{return &Cache{Expiration:time.Now().Add(ttl),Data:data}};func(c *Cache)Expired()bool{return time.Now().After(c.Expiration)};func(c *Cache)Bind(dst interface{})error{if err:=mapstructure.Decode(c.Data,dst);err!=nil{return err};return nil};func(c *Cache)Write(w io.Writer)error{buf,err:=json.Marshal(c);if err!=nil{return err};if _,err:=w.Write(buf);err!=nil{return err};return nil};
Loading
Loading