add jx-import doc
This commit is contained in:
parent
cf49268a25
commit
369cb7fde5
@ -43,6 +43,11 @@ Import the contents of a tar archive into a resource document.
|
|||||||
|
|
||||||
`jx import ./test.tgz`
|
`jx import ./test.tgz`
|
||||||
|
|
||||||
|
|
||||||
|
`jx import repo/packages/build.jx.yaml ./gopkgs.tar.gz`
|
||||||
|
|
||||||
|
![Import Doc](md-images/jx-import.gif)
|
||||||
|
|
||||||
Read a resource document from an http endpoint.
|
Read a resource document from an http endpoint.
|
||||||
|
|
||||||
`jx import http://localhost/resources`
|
`jx import http://localhost/resources`
|
||||||
|
@ -125,6 +125,7 @@ func ImportSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer outputTarget.Close()
|
||||||
|
|
||||||
if len(documents) == 0 {
|
if len(documents) == 0 {
|
||||||
documents = append(documents, resource.NewDocument())
|
documents = append(documents, resource.NewDocument())
|
||||||
|
@ -27,10 +27,11 @@ type DeclFile struct {
|
|||||||
Gzip bool `yaml:"gzip,omitempty" json:"gzip,omitempty"`
|
Gzip bool `yaml:"gzip,omitempty" json:"gzip,omitempty"`
|
||||||
Format string `yaml:"format,omitempty" json:"format,omitempty"`
|
Format string `yaml:"format,omitempty" json:"format,omitempty"`
|
||||||
encoder resource.Encoder `yaml:"-" json:"-"`
|
encoder resource.Encoder `yaml:"-" json:"-"`
|
||||||
|
closer func() error `yaml:"-" json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDeclFile() *DeclFile {
|
func NewDeclFile() *DeclFile {
|
||||||
return &DeclFile{ Gzip: false }
|
return &DeclFile{ Gzip: false, closer: func() error { return nil } }
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFileDocTarget(u *url.URL, format string, gzip bool, fileUri bool) DocTarget {
|
func NewFileDocTarget(u *url.URL, format string, gzip bool, fileUri bool) DocTarget {
|
||||||
@ -43,6 +44,7 @@ func NewFileDocTarget(u *url.URL, format string, gzip bool, fileUri bool) DocTar
|
|||||||
} else {
|
} else {
|
||||||
t.Path = filepath.Join(u.Hostname(), u.Path)
|
t.Path = filepath.Join(u.Hostname(), u.Path)
|
||||||
}
|
}
|
||||||
|
t.Open()
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +67,7 @@ func init() {
|
|||||||
t.Format = FormatJson
|
t.Format = FormatJson
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
t.Open()
|
||||||
return t
|
return t
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -102,13 +105,10 @@ func init() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DeclFile) Open() error {
|
||||||
func (d *DeclFile) Type() string { return "decl" }
|
|
||||||
|
|
||||||
func (d *DeclFile) EmitResources(documents []*resource.Document, filter resource.ResourceSelector) (error) {
|
|
||||||
var file *os.File
|
var file *os.File
|
||||||
var fileErr error
|
var fileErr error
|
||||||
var fileWriter io.Writer
|
var fileWriter io.WriteCloser
|
||||||
if d.Path == "" || d.Path == "-" {
|
if d.Path == "" || d.Path == "-" {
|
||||||
file = os.Stdout
|
file = os.Stdout
|
||||||
} else {
|
} else {
|
||||||
@ -116,9 +116,14 @@ func (d *DeclFile) EmitResources(documents []*resource.Document, filter resource
|
|||||||
if fileErr != nil {
|
if fileErr != nil {
|
||||||
return fileErr
|
return fileErr
|
||||||
}
|
}
|
||||||
defer func() {
|
d.closer = func() error {
|
||||||
|
d.encoder.Close()
|
||||||
|
fileWriter.Close()
|
||||||
|
if file != fileWriter {
|
||||||
file.Close()
|
file.Close()
|
||||||
}()
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.Gzip {
|
if d.Gzip {
|
||||||
@ -136,6 +141,16 @@ func (d *DeclFile) EmitResources(documents []*resource.Document, filter resource
|
|||||||
d.encoder = resource.NewYAMLEncoder(fileWriter)
|
d.encoder = resource.NewYAMLEncoder(fileWriter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DeclFile) Close() error {
|
||||||
|
return d.closer()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DeclFile) Type() string { return "decl" }
|
||||||
|
|
||||||
|
func (d *DeclFile) EmitResources(documents []*resource.Document, filter resource.ResourceSelector) (error) {
|
||||||
for _, doc := range documents {
|
for _, doc := range documents {
|
||||||
emitDoc := resource.NewDocument()
|
emitDoc := resource.NewDocument()
|
||||||
if validationErr := doc.Validate(); validationErr != nil {
|
if validationErr := doc.Validate(); validationErr != nil {
|
||||||
|
@ -24,6 +24,7 @@ type DocTarget interface {
|
|||||||
Type() string
|
Type() string
|
||||||
|
|
||||||
EmitResources(documents []*resource.Document, filter resource.ResourceSelector) error
|
EmitResources(documents []*resource.Document, filter resource.ResourceSelector) error
|
||||||
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDocTarget(uri string) DocTarget {
|
func NewDocTarget(uri string) DocTarget {
|
||||||
|
@ -22,16 +22,19 @@ _ "regexp"
|
|||||||
type Tar struct {
|
type Tar struct {
|
||||||
Path string `yaml:"path" json:"path"`
|
Path string `yaml:"path" json:"path"`
|
||||||
Gzip bool `yaml:"gzip" json:"gzip"`
|
Gzip bool `yaml:"gzip" json:"gzip"`
|
||||||
|
writer *tar.Writer `yaml:"-" json:"-"`
|
||||||
|
closer func() error `yaml:"-" json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTar() *Tar {
|
func NewTar() *Tar {
|
||||||
return &Tar{ Gzip: false }
|
return &Tar{ Gzip: false, closer: func() error { return nil } }
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
TargetTypes.Register([]string{"tar"}, func(u *url.URL) DocTarget {
|
TargetTypes.Register([]string{"tar"}, func(u *url.URL) DocTarget {
|
||||||
t := NewTar()
|
t := NewTar()
|
||||||
t.Path,_ = filepath.Abs(filepath.Join(u.Hostname(), u.Path))
|
t.Path,_ = filepath.Abs(filepath.Join(u.Hostname(), u.Path))
|
||||||
|
t.Open()
|
||||||
return t
|
return t
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -44,15 +47,13 @@ func init() {
|
|||||||
t.Path = filepath.Join(u.Hostname(), u.Path)
|
t.Path = filepath.Join(u.Hostname(), u.Path)
|
||||||
}
|
}
|
||||||
t.Gzip = true
|
t.Gzip = true
|
||||||
|
t.Open()
|
||||||
return t
|
return t
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Tar) Open() error {
|
||||||
func (t *Tar) Type() string { return "tar" }
|
|
||||||
|
|
||||||
func (t *Tar) EmitResources(documents []*resource.Document, filter resource.ResourceSelector) error {
|
|
||||||
file, fileErr := os.Create(t.Path)
|
file, fileErr := os.Create(t.Path)
|
||||||
if fileErr != nil {
|
if fileErr != nil {
|
||||||
return fileErr
|
return fileErr
|
||||||
@ -64,13 +65,22 @@ func (t *Tar) EmitResources(documents []*resource.Document, filter resource.Reso
|
|||||||
fileWriter = file
|
fileWriter = file
|
||||||
}
|
}
|
||||||
|
|
||||||
tarWriter := tar.NewWriter(fileWriter)
|
t.writer = tar.NewWriter(fileWriter)
|
||||||
defer func() {
|
t.closer = func() error {
|
||||||
tarWriter.Close()
|
t.writer.Close()
|
||||||
fileWriter.Close()
|
fileWriter.Close()
|
||||||
file.Close()
|
return file.Close()
|
||||||
}()
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tar) Close() error {
|
||||||
|
return t.closer()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tar) Type() string { return "tar" }
|
||||||
|
|
||||||
|
func (t *Tar) EmitResources(documents []*resource.Document, filter resource.ResourceSelector) error {
|
||||||
for _,document := range documents {
|
for _,document := range documents {
|
||||||
for _,res := range document.Filter(func(d *resource.Declaration) bool {
|
for _,res := range document.Filter(func(d *resource.Declaration) bool {
|
||||||
return d.Type == "file"
|
return d.Type == "file"
|
||||||
@ -79,10 +89,10 @@ func (t *Tar) EmitResources(documents []*resource.Document, filter resource.Reso
|
|||||||
slog.Info("Tar.EmitResources", "file", f)
|
slog.Info("Tar.EmitResources", "file", f)
|
||||||
hdr, fiErr := tar.FileInfoHeader(f.FileInfo(), "")
|
hdr, fiErr := tar.FileInfoHeader(f.FileInfo(), "")
|
||||||
slog.Info("Tar.EmitResources", "header", hdr, "err", fiErr)
|
slog.Info("Tar.EmitResources", "header", hdr, "err", fiErr)
|
||||||
if err := tarWriter.WriteHeader(hdr); err != nil {
|
if err := t.writer.WriteHeader(hdr); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if _, err := tarWriter.Write([]byte(f.Content)); err != nil {
|
if _, err := t.writer.Write([]byte(f.Content)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ type MockDocTarget struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockDocTarget) Type() string { return m.InjectType() }
|
func (m *MockDocTarget) Type() string { return m.InjectType() }
|
||||||
|
func (m *MockDocTarget) Close() error { return nil }
|
||||||
func (m *MockDocTarget) EmitResources(documents []*resource.Document, filter resource.ResourceSelector) error { return m.InjectEmitResources(documents, filter) }
|
func (m *MockDocTarget) EmitResources(documents []*resource.Document, filter resource.ResourceSelector) error { return m.InjectEmitResources(documents, filter) }
|
||||||
|
|
||||||
func NewFooDocTarget() DocTarget {
|
func NewFooDocTarget() DocTarget {
|
||||||
|
BIN
md-images/jx-import.gif
Normal file
BIN
md-images/jx-import.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 442 KiB |
Loading…
Reference in New Issue
Block a user