11package pipeline
22
33import (
4- "errors"
5- "fmt"
64 "io/ioutil"
75 "net"
86 "net/http"
@@ -15,8 +13,6 @@ import (
1513 "github.com/artefactual-labs/enduro/internal/amclient"
1614)
1715
18- // TODO: PipelineRegistry should return Pipeline objects!
19-
2016type Config struct {
2117 Name string
2218 BaseURL string
@@ -28,68 +24,36 @@ type Config struct {
2824 ProcessingConfig string
2925}
3026
31- // PipelineRegistry is a collection of known pipelines.
32- type PipelineRegistry struct {
33- pipelines map [ string ] Config
27+ type Pipeline struct {
28+ config * Config
29+ client * http. Client
3430}
3531
36- func NewPipelineRegistry (configs []Config ) * PipelineRegistry {
37- pipelines := map [string ]Config {}
38- for _ , cfg := range configs {
39- cfg .TransferDir = expandPath (cfg .TransferDir )
40- pipelines [cfg .Name ] = cfg
32+ func NewPipeline (config * Config ) * Pipeline {
33+ config .TransferDir = expandPath (config .TransferDir )
34+ config .ProcessingDir = expandPath (config .ProcessingDir )
4135
42- }
43- return & PipelineRegistry {
44- pipelines : pipelines ,
36+ return & Pipeline {
37+ config : config ,
38+ client : httpClient () ,
4539 }
4640}
4741
48- func (p PipelineRegistry ) Config (name string ) (* Config , error ) {
49- cfg , ok := p .pipelines [name ]
50- if ! ok {
51- return nil , errors .New ("client not found" )
52- }
53-
54- return & cfg , nil
42+ // Client returns the Archivematica API client ready for use.
43+ func (p Pipeline ) Client () * amclient.Client {
44+ return amclient .NewClient (p .client , p .config .BaseURL , p .config .User , p .config .Key )
5545}
5646
57- func ( p PipelineRegistry ) Client ( name string ) ( * amclient. Client , error ) {
58- cfg , err := p . Config ( name )
59- if err != nil {
60- return nil , fmt . Errorf ( "error fetching pipeline configuration: %w" , err )
47+ // TempFile creates a temporary file in the processing directory.
48+ func ( p Pipeline ) TempFile ( pattern string ) ( * os. File , error ) {
49+ if pattern == "" {
50+ pattern = "blob-*"
6151 }
62-
63- client , err := amclient .New (httpClient (), cfg .BaseURL , cfg .User , cfg .Key )
64- if err != nil {
65- return nil , fmt .Errorf ("error creating Archivematica API client: %w" , err )
66- }
67-
68- return client , nil
52+ return ioutil .TempFile (p .config .ProcessingDir , pattern )
6953}
7054
71- // TempFile returns a new temporary file inside the processing directory of the
72- // given pipeline.
73- func (p PipelineRegistry ) TempFile (name string ) (* os.File , error ) {
74- cfg , err := p .Config (name )
75- if err != nil {
76- return nil , fmt .Errorf ("error fetching pipeline configuration: %w" , err )
77- }
78-
79- return ioutil .TempFile (cfg .ProcessingDir , "blob-*" )
80- }
81-
82- func expandPath (path string ) string {
83- usr , _ := user .Current ()
84- dir := usr .HomeDir
85-
86- if path == "~" {
87- path = dir
88- } else if strings .HasPrefix (path , "~/" ) {
89- path = filepath .Join (dir , path [2 :])
90- }
91-
92- return path
55+ func (p Pipeline ) Config () * Config {
56+ return p .config
9357}
9458
9559func httpClient () * http.Client {
@@ -110,3 +74,16 @@ func httpClient() *http.Client {
11074 Transport : transport ,
11175 }
11276}
77+
78+ func expandPath (path string ) string {
79+ usr , _ := user .Current ()
80+ dir := usr .HomeDir
81+
82+ if path == "~" {
83+ path = dir
84+ } else if strings .HasPrefix (path , "~/" ) {
85+ path = filepath .Join (dir , path [2 :])
86+ }
87+
88+ return path
89+ }
0 commit comments