Skip to content

Commit

Permalink
Pull schemes directly from GitHub for CI (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
belak authored Jun 28, 2022
1 parent 40afe22 commit 85c8ab5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ ARGS=""

if [[ -n $INPUT_PATH ]]; then
ARGS+="-schemes-dir $INPUT_PATH"
else
ARGS+="-online"
fi

/bin/base16-builder-go $ARGS
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.18

require (
github.com/cbroglie/mustache v1.3.1
github.com/nlepage/go-tarfs v1.1.0
github.com/sirupsen/logrus v1.8.1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/stretchr/testify v1.7.1 // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nlepage/go-tarfs v1.1.0 h1:bsACOiZMB/zFjYG/sE01070i9Fl26MnRpw0L6WuyfVs=
github.com/nlepage/go-tarfs v1.1.0/go.mod h1:IhxRcLhLkawBetnwu/JNuoPkq/6cclAllhgEa6SmzS8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
Expand Down
43 changes: 41 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"compress/gzip"
"embed"
"flag"
"io/fs"
"net/http"
"os"

"github.com/nlepage/go-tarfs"
"github.com/sirupsen/logrus"
)

Expand All @@ -15,6 +18,7 @@ var schemesFS embed.FS
var (
schemesDir string
templateDir string
onlineMode bool

// Define the logger we'll be using
logVerbose bool
Expand All @@ -31,14 +35,40 @@ func init() {
flag.StringVar(&schemesDir, "schemes-dir", "-", "Target directory for scheme data. The default value uses internal schemes.")
flag.StringVar(&templateDir, "template-dir", ".", "Target template directory to build.")
flag.BoolVar(&logVerbose, "verbose", false, "Log all debug messages")
flag.BoolVar(&onlineMode, "online", false, "Run in online mode and pull schemes directly from GitHub")

rawLog.Level = logrus.InfoLevel
if logVerbose {
rawLog.Level = logrus.DebugLevel
}
}

func getSchemesFromGithub() (fs.FS, error) {
log.Info("Attempting to load schemes from GitHub")

r, err := http.Get("https://github.com/base16-project/base16-schemes/archive/refs/heads/main.tar.gz")
if err != nil {
return nil, err
}

gzipReader, err := gzip.NewReader(r.Body)
if err != nil {
return nil, err
}

targetFS, err := tarfs.New(gzipReader)
if err != nil {
return nil, err
}

// The archive has a subfolder containing all the schemes, so we return a
// subfs of the folder.
return fs.Sub(targetFS, "base16-schemes-main")
}

func main() {
var err error

flag.Parse()

log.WithFields(logrus.Fields{
Expand All @@ -49,8 +79,17 @@ func main() {

var targetFS fs.FS
if schemesDir == "-" {
log.Info("Using internal schemes")
targetFS, _ = fs.Sub(schemesFS, "schemes")
// If we're in online mode, the default is to pull from GitHub,
// otherwise use the embedded schemes.
if onlineMode {
targetFS, err = getSchemesFromGithub()
if err != nil {
log.WithError(err).Fatal("Failed to load schemes from GitHub")
}
} else {
log.Info("Using internal schemes")
targetFS, _ = fs.Sub(schemesFS, "schemes")
}
} else {
log.Infof("Processing scheme dir %q", schemesDir)
targetFS = os.DirFS(schemesDir)
Expand Down

0 comments on commit 85c8ab5

Please sign in to comment.