Skip to content

Commit

Permalink
Merge pull request #1 from sigmonsays/initial-wip
Browse files Browse the repository at this point in the history
Initial wip
  • Loading branch information
sigmonsays authored Nov 21, 2022
2 parents 5499bd3 + f4aa15e commit c5388eb
Show file tree
Hide file tree
Showing 16 changed files with 472 additions and 1 deletion.
115 changes: 115 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
name: Build debian package (.deb)

on:
push:
#branches: [ master ]
tags: [ 'v*' ]

jobs:

build:
runs-on: ubuntu-latest
strategy:
matrix:
codename: [focal]
steps:
- uses: actions/checkout@v2

- name: Unshallow
run: git fetch --prune --unshallow

- name: Build debian package
uses: sigmonsays/dpkg-buildpackage-go@master

- name: list files
run: ls -l *.deb

- name: Upload result
uses: actions/upload-artifact@v2
with:
name: ${{ matrix.codename }}
path: |
*.deb
test:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Download artifact
uses: actions/download-artifact@v2
with:
name: ${{ matrix.codename }}
path: artifact

release:
needs: test
runs-on: ubuntu-latest
steps:
- name: Create release
id: create_release
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
commitish: ${{ github.sha }}
draft: false
prerelease: false
outputs:
upload_url: ${{ steps.create_release.outputs.upload_url }}

upload:
needs: release
runs-on: ubuntu-latest
strategy:
matrix:
codename: [focal]
steps:
- name: Download artifact
uses: actions/download-artifact@v2
with:
name: ${{ matrix.codename }}
path: ${{ matrix.codename }}
- name: list files
shell: bash
run: find

- name: Get Name of Artifact
run: |
ARTIFACT_PATHNAME=$(ls ${{ matrix.codename }}/*.deb | head -n 1)
ARTIFACT_NAME=$(basename $ARTIFACT_PATHNAME)
echo "ARTIFACT_PATHNAME=${ARTIFACT_PATHNAME}" >> $GITHUB_ENV
echo "ARTIFACT_NAME=${ARTIFACT_NAME}" >> $GITHUB_ENV
- name: Upload release asset
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ needs.release.outputs.upload_url }}
# asset_path: ${{ matrix.codename }}
# asset_name: ${{ matrix.codename }}
asset_path: ${{ env.ARTIFACT_PATHNAME }}
asset_name: ${{ env.ARTIFACT_NAME }}
asset_content_type: application/deb

release-binaries:
name: release linux/amd64
needs: release
runs-on: ubuntu-latest
strategy:
matrix:
goos: [linux, windows, darwin]
goarch: ["386", amd64]
steps:
- uses: actions/checkout@v2
- uses: wangyoucao577/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
#project_path: .
binary_name: dotbot
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
#goversion: "https://dl.google.com/go/go1.13.1.linux-amd64.tar.gz"
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# dotbot
# dotbot
1 change: 1 addition & 0 deletions VERSION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.2
76 changes: 76 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"bytes"
"fmt"
"os"

yaml "gopkg.in/yaml.v3"
)

// main configuration structure
type AppConfig struct {
Symlinks map[string]string `yaml:"symlinks"`
}

func (c *AppConfig) LoadYaml(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}

b := bytes.NewBuffer(nil)
_, err = b.ReadFrom(f)
if err != nil {
return err
}

if err := c.LoadYamlBuffer(b.Bytes()); err != nil {
return err
}

if err := c.FixupConfig(); err != nil {
return err
}

return nil
}

func (c *AppConfig) LoadYamlBuffer(buf []byte) error {
err := yaml.Unmarshal(buf, c)
if err != nil {
return err
}
return nil
}

func (me *AppConfig) PrintConfig() {
d, err := yaml.Marshal(me)
if err != nil {
fmt.Println("Marshal error", err)
return
}
fmt.Println("-- Configuration --")
fmt.Println(string(d))
}

func GetDefaultConfig() *AppConfig {
return &AppConfig{}
}

func (c *AppConfig) LoadDefault() {
*c = *GetDefaultConfig()
}

// after loading configuration this gives us a spot to "fix up" any configuration
// or abort the loading process
func (c *AppConfig) FixupConfig() error {
// var emptyConfig AppConfig

return nil
}

func PrintDefaultConfig() {
conf := GetDefaultConfig()
conf.PrintConfig()
}
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dotbot (0.0.1) UNRELEASED; urgency=medium

[ sig ]
* dotbot

-- d <u@d1> Fri, 05 Feb 2021 12:38:35 -0800
1 change: 1 addition & 0 deletions debian/compat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9
12 changes: 12 additions & 0 deletions debian/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Source: dotbot
Section: go
Priority: extra
Maintainer: Sig Lange <[email protected]>
Build-Depends: debhelper (>= 9)
Standards-Version: 3.9.5

Package: dotbot
Architecture: any
Pre-Depends: dpkg (>= 1.16.1), ${misc:Pre-Depends}
Depends: ${misc:Depends}
Description: dotbot
7 changes: 7 additions & 0 deletions debian/rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/make -f

%:
dh $@
override_dh_usrlocal:
# ignore dh_usrlocal

4 changes: 4 additions & 0 deletions dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh

alias gi='go install github.com/sigmonsays/dotbot'
alias t='dotbot'
15 changes: 15 additions & 0 deletions etc/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

symlinks:
~/.aws: .aws
~/.bash_core: .bash_core
~/.bash_profile: .bash_profile
~/.bashrc: .bashrc
~/.bashrc.d: .bashrc.d
~/.inputrc: .inputrc
~/bin: bin
~/.gitconfig: .gitconfig
~/.profile: .profile
~/.tmux.conf: .tmux.conf


# EOF
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
module github.com/sigmonsays/dotbot

go 1.18

require (
github.com/sigmonsays/go-logging v0.0.0-20220128052735-680ed4f96095
github.com/urfave/cli/v2 v2.23.5
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sigmonsays/go-logging v0.0.0-20220128052735-680ed4f96095 h1:xCly5jE+yNrptrLVDO8XwLiB8fPr8EnjF90EUP/5nEc=
github.com/sigmonsays/go-logging v0.0.0-20220128052735-680ed4f96095/go.mod h1:i1dZK/LIPzZMGyZCwV71F6pg3JkZdbqBsBnhr6GpPQ8=
github.com/urfave/cli/v2 v2.23.5 h1:xbrU7tAYviSpqeR3X4nEFWUdB/uDZ6DE+HxmRU7Xtyw=
github.com/urfave/cli/v2 v2.23.5/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
113 changes: 113 additions & 0 deletions link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"os"
"os/user"
"path/filepath"
"strings"

"github.com/urfave/cli/v2"
)

type Link struct {
ctx *Context
}

func (me *Link) Flags() []cli.Flag {
return []cli.Flag{
&cli.StringSliceFlag{
Name: "config",
Usage: "config file",
Aliases: []string{"c"},
},
}
return nil
}
func (me *Link) Run(c *cli.Context) error {

configfiles := c.StringSlice("config")
log.Tracef("%d files to execute", len(configfiles))

for _, filename := range configfiles {
err := me.RunFile(filename)
if err != nil {
log.Warnf("RunFile %s: %s", filename, err)
}
}

return nil
}

func (me *Link) RunFile(path string) error {
log.Tracef("runfile %s", path)
cfg := GetDefaultConfig()
err := cfg.LoadYaml(path)
if err != nil {
return err
}
if log.IsTrace() {
cfg.PrintConfig()
}

usr, _ := user.Current()
homedir := usr.HomeDir
log.Tracef("homedir is %s", homedir)

for target, link := range cfg.Symlinks {

// resolve target tilde prefix
if strings.HasPrefix(target, "~/") {
target = filepath.Join(homedir, target[2:])
}

abslink, err := filepath.Abs(link)
if err != nil {
log.Warnf("Abs %s: %s", link, err)
continue
}
log.Tracef("symlink %s to %s", abslink, target)
log.Tracef("abslink %s", abslink)
log.Tracef("target %s", target)

// stat dest and check if destination exists and is a symlink
dest, err := os.Lstat(target)
dest_exists := (err == nil)
log.Tracef("target exists %v", dest_exists)
is_symlink := false
pointsto := ""
if dest_exists && dest.Mode()&os.ModeSymlink != 0 {
is_symlink = true
link, err := os.Readlink(target)
if err == nil {
pointsto = link
} else {
log.Warnf("Readlink %s", err)
}
}

is_valid := true
if is_symlink {
// evaluate what it points to
if pointsto != abslink {
is_valid = false
log.Tracef("invalid link points to %s but should be %s", abslink, pointsto)
}

}

if dest_exists && is_symlink && is_valid {
log.Tracef("%s already created", abslink)
log.Tracef("points to %s", pointsto)
continue
}

err = os.Symlink(abslink, target)
if err != nil {
log.Warnf("Symlink %s", err)
continue
}
log.Infof("symlink %s", target)

}
return nil
}
Loading

0 comments on commit c5388eb

Please sign in to comment.