Skip to content

Commit

Permalink
Dotfiles done
Browse files Browse the repository at this point in the history
  • Loading branch information
Keloran committed Aug 10, 2020
1 parent 2bb11be commit 6935318
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
# vendor/

dotfilesLoader
testbed/
47 changes: 36 additions & 11 deletions dots/dots.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import (
)

type Dots struct {
Username string
Github files.Github
Force bool
Skip bool
Username string
Github files.Github
Force bool
Skip bool
CurrentDir string
}

func (d Dots) Install() error {
type dots struct {
Location string
Name string
}

if d.Github.Repository != "" {
filesLocation, err := files.Downloader{
GithubDetails: d.Github,
Expand All @@ -32,15 +38,34 @@ func (d Dots) Install() error {
if err != nil {
return fmt.Errorf("dotfiles extract: %w", err)
}
fmt.Printf("Files: %+v", zipedFiles)

dd := []dots{}
prefixLen := len(zipedFiles[0])
for _, file := range zipedFiles[1:] {
if file[(prefixLen + 1):(prefixLen + 2)] == "." {
ddd := dots{
Location: file,
Name: file[(prefixLen + 1):],
}

dd = append(dd, ddd)
}
}

for _, dot := range dd {
err = files.Copy(dot.Location, fmt.Sprintf("%s/%s", d.CurrentDir, dot.Name))
if err != nil {
return fmt.Errorf("Dotfiles copy: %w", err)
}
}
}

err = files.Cleanup("github")
return fmt.Errorf("dotfiles installed cleanup: %w", err)
err = files.Cleanup("/tmp/github")
if err != nil {
return fmt.Errorf("dotfiles installed cleanup: %w", err)
}
}

fmt.Printf("Dots %+v\n", d)
return fmt.Errorf("tester")

// return nil
fmt.Print("dotfiles installed\n")
return nil
}
42 changes: 41 additions & 1 deletion files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package files

import (
"fmt"
"os"
"io"
"os"
)

func Cleanup(input string) error {
Expand All @@ -26,6 +27,7 @@ func Cleanup(input string) error {
if err != nil {
return fmt.Errorf("Files cleanup removeAll: %w", err)
}
return nil
}

if err = os.Remove(input); err != nil {
Expand All @@ -34,3 +36,41 @@ func Cleanup(input string) error {

return nil
}

func Copy(input, location string) error {
stat, err := os.Stat(input)
if err != nil {
return fmt.Errorf("Files copy stat: %w", err)
}
if !stat.Mode().IsRegular() {
return fmt.Errorf("Files copy not a file: %s", input)
}

src, err := os.Open(input)
if err != nil {
return fmt.Errorf("Files copy open: %w", err)
}
defer func() {
err := src.Close()
if err != nil {
fmt.Printf("Files copy close input: %v", err)
}
}()

dest, err := os.Create(location)
if err != nil {
return fmt.Errorf("Files copy create: %w", err)
}
defer func() {
err := dest.Close()
if err != nil {
fmt.Printf("Files copy close dest: %v", err)
}
}()
_, err = io.Copy(dest, src)
if err != nil {
return fmt.Errorf("Files copy copy: %w", err)
}

return nil
}
32 changes: 32 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"

"github.com/Keloran/dotfilesLoader/console"
)

func Help() {
hFormat := " %s %s"
fFormat := " -%s %s"

c := console.NewConsole(true)
c.Start("Help").
Info("Usage dotfiles <command> <flags>").
BlankLine().
Info("Commands:").
FormattedText(fmt.Sprintf(hFormat, console.Cyan("help"), console.Blue(" This message"))).
FormattedText(fmt.Sprintf(hFormat, console.Cyan("cli"), console.Blue(" Install CLI Apps"))).
FormattedText(fmt.Sprintf(hFormat, console.Cyan("gui"), console.Blue(" Install GUI Apps"))).
FormattedText(fmt.Sprintf(hFormat, console.Cyan("os"), console.Blue(" Install OS Settings"))).
FormattedText(fmt.Sprintf(hFormat, console.Cyan("dots"), console.Blue(" Install the dotfiles"))).
FormattedText(fmt.Sprintf(hFormat, console.Cyan("update"), console.Blue("Run updaters"))).
BlankLine().
Info("Flags:").
FormattedText(fmt.Sprintf(fFormat, console.Green("github-user | github-username <keloran>"), console.Blue(" Github username"))).
FormattedText(fmt.Sprintf(fFormat, console.Green("github-repository | github-repo <dotfiles>"), console.Blue("Github repository"))).
FormattedText(fmt.Sprintf(fFormat, console.Green("force"), console.Blue(" Force installs"))).
FormattedText(fmt.Sprintf(fFormat, console.Green("skip-download"), console.Blue(" Skip Download"))).
FormattedText(fmt.Sprintf(fFormat, console.Green("user <keloran>"), console.Blue(" The username if needs sudo"))).
End("")
}
60 changes: 25 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package main

import (
"flag"
"fmt"
"os"
"strings"

"github.com/Keloran/dotfilesLoader/console"
"github.com/Keloran/dotfilesLoader/dots"
"github.com/Keloran/dotfilesLoader/files"
"flag"
"fmt"
"os"
"strings"

"github.com/Keloran/dotfilesLoader/console"
"github.com/Keloran/dotfilesLoader/dots"
"github.com/Keloran/dotfilesLoader/files"
)

func main() {
currentLocation, err := os.Getwd()
if err != nil {
fmt.Printf("couldn't get current directory: %+v", err)
return
}

command := ""
if len(os.Args) >= 2 {
for _, arg := range os.Args {
Expand Down Expand Up @@ -65,10 +71,11 @@ func main() {

// dots
dot := dots.Dots{
Username: USERNAME,
Force: *force,
Skip: *skip,
Github: Github,
Username: USERNAME,
Force: *force,
Skip: *skip,
Github: Github,
CurrentDir: currentLocation,
}
if dotfiles {
err := dot.Install()
Expand All @@ -81,47 +88,30 @@ func main() {

// GUI
if appsGUI {
fmt.Print("Apps GUI\n")
return
}

// CLI
if appsCLI {
fmt.Print("Apps CLI\n")
return
}

// OS
if OS {
fmt.Print("OS\n")
return
}

// All
if all {
fmt.Print("All\n")
return
}

// Help
if help {
hFormat := " %s %s"
fFormat := " -%s %s"

c := console.NewConsole(true)
c.Start("Help").
Info("Usage dotfiles <command> <flags>").
BlankLine().
Info("Commands:").
FormattedText(fmt.Sprintf(hFormat, console.Cyan("help"), console.Blue(" This message"))).
FormattedText(fmt.Sprintf(hFormat, console.Cyan("cli"), console.Blue(" Install CLI Apps"))).
FormattedText(fmt.Sprintf(hFormat, console.Cyan("gui"), console.Blue(" Install GUI Apps"))).
FormattedText(fmt.Sprintf(hFormat, console.Cyan("os"), console.Blue(" Install OS Settings"))).
FormattedText(fmt.Sprintf(hFormat, console.Cyan("dots"), console.Blue(" Install the dotfiles"))).
FormattedText(fmt.Sprintf(hFormat, console.Cyan("update"), console.Blue("Run updaters"))).
BlankLine().
Info("Flags:").
FormattedText(fmt.Sprintf(fFormat, console.Green("github-user | github-username <keloran>"), console.Blue(" Github username"))).
FormattedText(fmt.Sprintf(fFormat, console.Green("github-repository | github-repo <dotfiles>"), console.Blue("Github repository"))).
FormattedText(fmt.Sprintf(fFormat, console.Green("force"), console.Blue(" Force installs"))).
FormattedText(fmt.Sprintf(fFormat, console.Green("skip-download"), console.Blue(" Skip Download"))).
FormattedText(fmt.Sprintf(fFormat, console.Green("user <keloran>"), console.Blue(" The username if needs sudo"))).
End("")
}
Help()
}
}

0 comments on commit 6935318

Please sign in to comment.