Add native output format

This commit is contained in:
adisbladis 2020-07-20 14:26:57 +02:00
parent bdf43e8d31
commit c2cd1e1fff
No known key found for this signature in database
GPG key ID: 110BFAD44C6249B7
4 changed files with 64 additions and 2 deletions

View file

@ -0,0 +1,57 @@
package gomod2nix
import (
"bytes"
"fmt"
"github.com/BurntSushi/toml"
"github.com/tweag/gomod2nix/types"
"io/ioutil"
)
type packageT struct {
Type string `toml:"type"`
URL string `toml:"url"`
Rev string `toml:"rev"`
Sha256 string `toml:"sha256"`
}
func Marshal(pkgs []*types.Package) ([]byte, error) {
result := make(map[string]*packageT)
for _, pkg := range pkgs {
result[pkg.GoPackagePath] = &packageT{
Type: "git",
URL: pkg.URL,
Rev: pkg.Rev,
Sha256: pkg.Sha256,
}
}
var buf bytes.Buffer
e := toml.NewEncoder(&buf)
err := e.Encode(result)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func LoadGomod2Nix(filePath string) map[string]*types.Package {
ret := make(map[string]*types.Package)
b, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println(err)
return ret
}
result := make(map[string]*packageT)
_, err = toml.Decode(string(b), &result)
if err != nil {
fmt.Println(err)
return ret
}
return ret
}

1
go.mod
View file

@ -3,6 +3,7 @@ module github.com/tweag/gomod2nix
go 1.14
require (
github.com/BurntSushi/toml v0.3.1
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

2
go.sum
View file

@ -1,3 +1,5 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
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=

View file

@ -4,7 +4,8 @@ import (
"flag"
"fmt"
"github.com/tweag/gomod2nix/fetch"
"github.com/tweag/gomod2nix/formats/buildgopackage"
// "github.com/tweag/gomod2nix/formats/buildgopackage"
"github.com/tweag/gomod2nix/formats/gomod2nix"
"path/filepath"
)
@ -21,7 +22,8 @@ func main() {
panic(err)
}
output, err := buildgopackage.Marshal(pkgs)
// output, err := buildgopackage.Marshal(pkgs)
output, err := gomod2nix.Marshal(pkgs)
if err != nil {
panic(err)
}