jsonnet-bundler/cmd/jb/main.go

228 lines
5.8 KiB
Go
Raw Normal View History

2018-04-24 15:19:40 +01:00
/*
Copyright 2018 jsonnet-bundler authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
2018-04-24 15:19:40 +01:00
"os"
"path"
"path/filepath"
"regexp"
"github.com/jsonnet-bundler/jsonnet-bundler/pkg"
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
"github.com/pkg/errors"
"gopkg.in/alecthomas/kingpin.v2"
2018-04-24 15:19:40 +01:00
)
const (
installActionName = "install"
initActionName = "init"
2018-04-24 15:19:40 +01:00
basePath = ".jsonnetpkg"
srcDirName = "src"
)
var (
availableSubcommands = []string{
initActionName,
installActionName,
2018-04-24 15:19:40 +01:00
}
2018-04-25 15:53:32 +01:00
githubSlugRegex = regexp.MustCompile("github.com/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)")
githubSlugWithVersionRegex = regexp.MustCompile("github.com/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)@(.*)")
githubSlugWithPathRegex = regexp.MustCompile("github.com/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/(.*)")
githubSlugWithPathAndVersionRegex = regexp.MustCompile("github.com/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/(.*)@(.*)")
2018-04-24 15:19:40 +01:00
)
func main() {
os.Exit(Main())
2018-04-24 15:19:40 +01:00
}
func Main() int {
cfg := struct {
JsonnetHome string
}{}
a := kingpin.New(filepath.Base(os.Args[0]), "A jsonnet package manager")
a.HelpFlag.Short('h')
a.Flag("jsonnetpkg-home", "The directory used to cache packages in.").
Default("vendor").StringVar(&cfg.JsonnetHome)
initCmd := a.Command(initActionName, "Initialize a new empty jsonnetfile")
installCmd := a.Command(installActionName, "Install all dependencies or install specific ones")
installCmdURLs := installCmd.Arg("packages", "URLs to package to install").URLList()
2018-04-24 15:19:40 +01:00
command, err := a.Parse(os.Args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, errors.Wrapf(err, "Error parsing commandline arguments"))
a.Usage(os.Args[1:])
return 2
}
2018-04-24 15:19:40 +01:00
switch command {
case initCmd.FullCommand():
return initCommand()
case installCmd.FullCommand():
return installCommand(cfg.JsonnetHome, *installCmdURLs...)
default:
installCommand(cfg.JsonnetHome)
2018-04-24 15:19:40 +01:00
}
return 0
}
func initCommand() int {
err := ioutil.WriteFile(pkg.JsonnetFile, []byte("{}"), 0644)
2018-04-24 15:19:40 +01:00
if err != nil {
kingpin.Fatalf("Failed to write new jsonnetfile.json: %v", err)
2018-04-24 15:19:40 +01:00
return 1
}
return 0
}
func installCommand(jsonnetHome string, urls ...*url.URL) int {
m, err := pkg.LoadJsonnetfile(pkg.JsonnetFile)
if err != nil {
kingpin.Fatalf("failed to load jsonnetfile: %v", err)
return 1
}
2018-04-28 18:01:00 +01:00
if len(urls) > 0 {
for _, url := range urls {
2018-04-24 15:19:40 +01:00
// install package specified in command
// $ jsonnetpkg install ksonnet git@github.com:ksonnet/ksonnet-lib
// $ jsonnetpkg install grafonnet git@github.com:grafana/grafonnet-lib grafonnet
// $ jsonnetpkg install github.com/grafana/grafonnet-lib/grafonnet
//
// github.com/(slug)/(dir)
urlString := url.String()
if githubSlugRegex.MatchString(urlString) {
2018-04-24 15:19:40 +01:00
name := ""
user := ""
repo := ""
subdir := ""
version := "master"
if githubSlugWithPathRegex.MatchString(urlString) {
if githubSlugWithPathAndVersionRegex.MatchString(urlString) {
matches := githubSlugWithPathAndVersionRegex.FindStringSubmatch(urlString)
2018-04-24 15:19:40 +01:00
user = matches[1]
repo = matches[2]
subdir = matches[3]
version = matches[4]
name = path.Base(subdir)
} else {
matches := githubSlugWithPathRegex.FindStringSubmatch(urlString)
2018-04-24 15:19:40 +01:00
user = matches[1]
repo = matches[2]
subdir = matches[3]
name = path.Base(subdir)
}
} else {
if githubSlugWithVersionRegex.MatchString(urlString) {
matches := githubSlugWithVersionRegex.FindStringSubmatch(urlString)
2018-04-24 15:19:40 +01:00
user = matches[1]
repo = matches[2]
name = repo
version = matches[3]
} else {
matches := githubSlugRegex.FindStringSubmatch(urlString)
2018-04-24 15:19:40 +01:00
user = matches[1]
repo = matches[2]
name = repo
}
}
newDep := spec.Dependency{
2018-04-24 15:19:40 +01:00
Name: name,
Source: spec.Source{
GitSource: &spec.GitSource{
2018-04-25 10:24:10 +01:00
Remote: fmt.Sprintf("https://github.com/%s/%s", user, repo),
2018-04-24 15:19:40 +01:00
Subdir: subdir,
},
},
Version: version,
}
oldDeps := m.Dependencies
newDeps := []spec.Dependency{}
oldDepReplaced := false
for _, d := range oldDeps {
if d.Name == newDep.Name {
newDeps = append(newDeps, newDep)
oldDepReplaced = true
} else {
newDeps = append(newDeps, d)
}
}
2018-04-24 15:19:40 +01:00
if !oldDepReplaced {
newDeps = append(newDeps, newDep)
}
m.Dependencies = newDeps
} else {
kingpin.Errorf("ignoring unrecognized url: %s", url)
2018-04-24 15:19:40 +01:00
}
}
2018-04-28 18:01:00 +01:00
}
2018-04-24 15:19:40 +01:00
2018-04-28 18:01:00 +01:00
srcPath := filepath.Join(jsonnetHome)
err = os.MkdirAll(srcPath, os.ModePerm)
if err != nil {
kingpin.Fatalf("failed to create jsonnet home path: %v", err)
return 3
}
2018-04-24 15:19:40 +01:00
2018-04-28 18:01:00 +01:00
lock, err := pkg.Install(context.TODO(), m, jsonnetHome)
if err != nil {
kingpin.Fatalf("failed to install: %v", err)
return 3
}
2018-04-24 15:19:40 +01:00
2018-04-28 18:01:00 +01:00
b, err := json.MarshalIndent(m, "", " ")
if err != nil {
kingpin.Fatalf("failed to encode jsonnet file: %v", err)
return 3
}
2018-04-24 15:19:40 +01:00
2018-04-28 18:01:00 +01:00
err = ioutil.WriteFile(pkg.JsonnetFile, b, 0644)
if err != nil {
kingpin.Fatalf("failed to write jsonnet file: %v", err)
return 3
}
2018-04-24 15:19:40 +01:00
2018-04-28 18:01:00 +01:00
b, err = json.MarshalIndent(lock, "", " ")
if err != nil {
kingpin.Fatalf("failed to encode jsonnet file: %v", err)
return 3
}
2018-04-24 15:19:40 +01:00
2018-04-28 18:01:00 +01:00
err = ioutil.WriteFile(pkg.JsonnetLockFile, b, 0644)
if err != nil {
kingpin.Fatalf("failed to write lock file: %v", err)
return 3
2018-04-24 15:19:40 +01:00
}
return 0
2018-04-24 15:19:40 +01:00
}