Add support for old deps.nix as an output format

This commit is contained in:
adisbladis 2020-07-20 13:55:32 +02:00
parent 678a0b17c4
commit bdf43e8d31
No known key found for this signature in database
GPG key ID: 110BFAD44C6249B7
6 changed files with 127 additions and 5 deletions

View file

@ -156,8 +156,8 @@ func fetchPackage(importPath string, goPackagePath string, rev string) (*types.P
// older output file (as the revs) don't match
//
// This is used to skip fetching where the previous package path & rev are still the same
Rev: rev,
Hash: fmt.Sprintf("sha256:%s", output.Sha256),
Rev: rev,
Sha256: output.Sha256,
}, nil
}

View file

@ -0,0 +1,95 @@
package buildgopackage
import (
"fmt"
"github.com/orivej/go-nix/nix/eval"
"github.com/orivej/go-nix/nix/parser"
"github.com/tweag/gomod2nix/types"
"log"
"os"
"strings"
)
const depNixFormat = ` {
goPackagePath = "%s";
fetch = {
type = "%s";
url = "%s";
rev = "%s";
sha256 = "%s";
};
}`
func Marshal(pkgs []*types.Package) ([]byte, error) {
var result []string
result = append(result, "[")
for _, pkg := range pkgs {
result = append(result,
fmt.Sprintf(depNixFormat,
pkg.GoPackagePath, "git", pkg.URL,
pkg.Rev, pkg.Sha256))
}
result = append(result, "]")
return []byte(strings.Join(result, "\n")), nil
}
// Load the contents of deps.nix into a struct
// This is mean to achieve re-use of previous invocations using the deps.nix (buildGoPackage) output format
func LoadDepsNix(filePath string) map[string]*types.Package {
ret := make(map[string]*types.Package)
stat, err := os.Stat(filePath)
if err != nil {
return ret
}
if stat.Size() == 0 {
return ret
}
p, err := parser.ParseFile(filePath)
if err != nil {
log.Println("Failed reading deps.nix")
return ret
}
evalResult := eval.ParseResult(p)
for _, pkgAttrsExpr := range evalResult.(eval.List) {
pkgAttrs, ok := pkgAttrsExpr.Eval().(eval.Set)
if !ok {
continue
}
fetch, ok := pkgAttrs[eval.Intern("fetch")].Eval().(eval.Set)
if !ok {
continue
}
goPackagePath, ok := pkgAttrs[eval.Intern("goPackagePath")].Eval().(string)
if !ok {
continue
}
url, ok := fetch[eval.Intern("url")].Eval().(string)
if !ok {
continue
}
rev, ok := fetch[eval.Intern("rev")].Eval().(string)
if !ok {
continue
}
sha256, ok := fetch[eval.Intern("sha256")].Eval().(string)
if !ok {
continue
}
ret[goPackagePath] = &types.Package{
GoPackagePath: goPackagePath,
URL: url,
Rev: rev,
Sha256: sha256,
}
}
return ret
}

1
go.mod
View file

@ -3,6 +3,7 @@ module github.com/tweag/gomod2nix
go 1.14
require (
github.com/orivej/go-nix v0.0.0-20180830055821-dae45d921a44
golang.org/x/mod v0.3.0
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e
)

22
go.sum
View file

@ -1,3 +1,24 @@
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI=
github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721 h1:JHZL0hZKJ1VENNfmXvHbgYlbUOvpzYzvy2aZU5gXVeo=
github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0=
github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE=
github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897 h1:p9Sln00KOTlrYkxI1zYWl1QLnEqAqEARBEYa8FQnQcY=
github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/orivej/e v0.0.0-20180728214217-ac3492690fda h1:fqLgbcmo9qKecZOH8lByuxi9XXoIhNYBpRJEo4rDEUQ=
github.com/orivej/e v0.0.0-20180728214217-ac3492690fda/go.mod h1:eOxOguJBxQH6q/o7CZvmR+fh5v1LHH1sfohtgISSSFA=
github.com/orivej/go-nix v0.0.0-20180830055821-dae45d921a44 h1:XDJpMiCKWt8CIT2LE1QrF4DdrvI1WciSNUrnYtNewPo=
github.com/orivej/go-nix v0.0.0-20180830055821-dae45d921a44/go.mod h1:4SkaXpoQ0tQ0OIkGqU8ByPLANmTTTU1iWPDz7YXatSA=
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
@ -5,6 +26,7 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180828065106-d99a578cf41b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View file

@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"github.com/tweag/gomod2nix/fetch"
"github.com/tweag/gomod2nix/formats/buildgopackage"
"path/filepath"
)
@ -20,8 +21,11 @@ func main() {
panic(err)
}
for _, pkg := range pkgs {
fmt.Println(pkg)
output, err := buildgopackage.Marshal(pkgs)
if err != nil {
panic(err)
}
fmt.Println(string(output))
}

View file

@ -4,5 +4,5 @@ type Package struct {
GoPackagePath string
URL string
Rev string
Hash string
Sha256 string
}