Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Jan 12, 2022
0 parents commit 6f2ff56
Show file tree
Hide file tree
Showing 49 changed files with 6,509 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bin/*
afx
*.swp
vendor
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
ifdef DEBUG
GOFLAGS := -gcflags="-N -l"
else
GOFLAGS :=
endif

GO ?= go
TAGS :=
LDFLAGS :=

GIT_COMMIT = $(shell git rev-parse HEAD)
GIT_SHA = $(shell git rev-parse --short HEAD)
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null || echo "canary")
GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean")

LDFLAGS += -X github.com/b4b4r07/afx/cmd.BuildSHA=${GIT_SHA}
LDFLAGS += -X github.com/b4b4r07/afx/cmd.GitTreeState=${GIT_DIRTY}

ifneq ($(GIT_TAG),)
LDFLAGS += -X github.com/b4b4r07/afx/cmd.BuildTag=${GIT_TAG}
endif

all: build

.PHONY: build
build:
$(GO) install $(GOFLAGS) -ldflags '$(LDFLAGS)'
61 changes: 61 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"context"

"github.com/b4b4r07/afx/pkg/helpers/shell"
"github.com/b4b4r07/afx/pkg/templates"
"github.com/spf13/cobra"
)

type configCmd struct {
meta
}

var (
// configLong is long description of config command
configLong = templates.LongDesc(``)

// configExample is examples for config command
configExample = templates.Examples(`
# Normal
pkg config
`)
)

// newConfigCmd creates a new config command
func newConfigCmd() *cobra.Command {
c := &configCmd{}

configCmd := &cobra.Command{
Use: "config",
Short: "Configure HCL files",
Long: configLong,
Example: configExample,
Aliases: []string{"cfg", "configure"},
DisableFlagsInUseLine: true,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if err := c.meta.init(args); err != nil {
return err
}
if c.parseErr != nil {
return c.parseErr
}
return c.run(args)
},
}

return configCmd
}

func (c *configCmd) run(args []string) error {
path, err := getConfigPath()
if err != nil {
return err
}
vim := shell.New("vim", path)
return vim.Run(context.Background())
}
208 changes: 208 additions & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package cmd

import (
"context"
"fmt"
"io"
"net/url"
"os"
"strings"

"github.com/b4b4r07/afx/pkg/config"
"github.com/b4b4r07/afx/pkg/helpers/shell"
"github.com/b4b4r07/afx/pkg/helpers/spin"
"github.com/b4b4r07/afx/pkg/templates"
"github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl2/hclwrite"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)

type getCmd struct {
meta

spin *spin.Spinner
pkg config.Package

verbose bool
}

var (
// getLong is long description of get command
getLong = templates.LongDesc(`adfasfadsfsds`)

// getExample is examples for get command
getExample = templates.Examples(`
# This command gets the definition based on given link
pkg get https://github.com/b4b4r07/enhancd
`)
)

// newGetCmd creates a new get command
func newGetCmd() *cobra.Command {
c := &getCmd{}

getCmd := &cobra.Command{
Use: "get <URL>",
Short: "Get package from given URL",
Long: getLong,
Example: getExample,
DisableFlagsInUseLine: true,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := c.meta.init(args); err != nil {
return err
}
if c.parseErr != nil {
return c.parseErr
}
c.Env.Ask("GITHUB_TOKEN")
c.spin = spin.New("%s")
c.spin.Start()
defer c.spin.Stop()
return c.run(args)
},
PostRunE: func(cmd *cobra.Command, args []string) error {
c.spin.Stop()
c.pkg.GetType()
path, err := getConfigPath()
if err != nil {
return err
}
var fp io.Writer
fp, err = os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return err
}
if c.verbose {
fp = io.MultiWriter(os.Stdout, fp)
}
f := hclwrite.NewEmptyFile()
cfg := config.ConvertsFrom(c.pkg)
gohcl.EncodeIntoBody(&cfg, f.Body())
fmt.Fprintf(fp, "%s\n", f.Bytes())
vim := shell.New("vim", path)
return vim.Run(context.Background())
},
}

f := getCmd.Flags()
f.BoolVar(&c.verbose, "verbose", false, "output verbosely")

return getCmd
}

func (c *getCmd) run(args []string) error {
var pkg config.Package

arg := args[0]
u, err := url.Parse(arg)
if err != nil {
return err
}

switch u.Host {
case "github.com":
e := strings.Split(u.Path[1:len(u.Path)], "/")
github, err := config.NewGitHub(e[0], e[1])
if err != nil {
c.Env.Refresh()
return err
}
pkg = github
case "gist.github.com":
e := strings.Split(u.Path[1:len(u.Path)], "/")
gist, err := config.NewGist(e[0], e[1])
if err != nil {
c.Env.Refresh()
return err
}
pkg = gist
default:
return fmt.Errorf("%s: currently github is only allowed", arg)
}

if config.Defined(c.Packages, pkg) {
return fmt.Errorf("%s: already installed", pkg.GetSlug())
}

pathsCh := make(chan []string)
errCh := make(chan error)
go func() {
paths, err := pkg.Objects()
pathsCh <- paths
errCh <- err
}()

result, err := c.prompt(promptui.Select{
Label: "Select package type",
Items: []string{"plugin", "command"},
})
if err != nil {
return err
}

paths := <-pathsCh
if err := <-errCh; err != nil {
return err
}

switch result {
case "command":
if len(pkg.GetCommandBlock().Link) == 0 {
from, _ := c.prompt(promptui.Select{
Label: "Select command file (source)",
Items: paths,
})
to, _ := c.prompt(promptui.Select{
Label: "Select command file (destination)",
Items: paths,
}, "(Rename to)")
pkg = pkg.SetCommand(config.Command{
Link: []*config.Link{&config.Link{From: from, To: to}},
})
}
case "plugin":
if len(paths) > 0 {
source, _ := c.prompt(promptui.Select{
Label: "Select source file",
Items: paths,
}, "(Others)")
pkg = pkg.SetPlugin(config.Plugin{Sources: []string{source}})
}
}

c.pkg = pkg
return nil
}

func (c getCmd) prompt(s promptui.Select, others ...string) (string, error) {
c.spin.Stop()
defer c.spin.Start()
s.HideSelected = true

if len(others) > 0 {
p := promptui.SelectWithAdd{
Label: s.Label.(string),
Items: s.Items.([]string),
AddLabel: others[0],
}
_, result, err := p.Run()
return result, err
}

switch items := s.Items.(type) {
case []string:
s.Searcher = func(input string, index int) bool {
item := items[index]
name := strings.Replace(strings.ToLower(item), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
}

_, result, err := s.Run()
return result, err
}
68 changes: 68 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"fmt"
"log"

"github.com/b4b4r07/afx/pkg/templates"
"github.com/spf13/cobra"
)

type initCmd struct {
meta

path string
}

var (
// initLong is long description of init command
initLong = templates.LongDesc(``)

// initExample is examples for init command
initExample = templates.Examples(`
# Start to use pkg
pkg init
`)
)

// newInitCmd creates a new init command
func newInitCmd() *cobra.Command {
c := &initCmd{}

initCmd := &cobra.Command{
Use: "init",
Short: "Initialize installed packages",
Long: initLong,
Example: initExample,
DisableFlagsInUseLine: true,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if err := c.meta.init(args); err != nil {
return err
}
if c.parseErr != nil {
return c.parseErr
}
return c.run(args)
},
}

return initCmd
}

func (c *initCmd) run(args []string) error {
for _, pkg := range c.Packages {
if err := pkg.Init(); err != nil {
log.Printf("[ERROR] %s: failed to init pacakge: %v\n", pkg.GetName(), err)
continue
}
}

if c.path != "" {
fmt.Printf("export PATH=$PATH:%s\n", c.path)
}

return nil
}
Loading

0 comments on commit 6f2ff56

Please sign in to comment.