-
Notifications
You must be signed in to change notification settings - Fork 2
/
importdescription.go
98 lines (83 loc) · 2.92 KB
/
importdescription.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Copyright 2016 Ontario Systems
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 isclib
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
const (
importFmtStr = `##class(%%SYSTEM.OBJ).ImportDir("%s","%s","%s",,%d)`
)
var (
// ErrTooManyRecursiveDirs is an error signifying too many ** are included in the glob pattern
ErrTooManyRecursiveDirs = errors.New("the glob must contain at most one **")
// ErrMissingPathSeparator is an error signifying a missing path separator in the glob pattern
ErrMissingPathSeparator = errors.New("there must be a path separator between ** and file pattern")
// ErrPathAfterRecursiveDirs is an error signifying that additional path information is included after the ** in the glob pattern
ErrPathAfterRecursiveDirs = errors.New("a ** must only be used as the last portion of the path before the file pattern")
// ErrWildcardInDirectory is an error signifying that a wildcard has been included in the directory part of the glob pattern
ErrWildcardInDirectory = errors.New("the directory portion of the glob must not contain *")
)
// ImportDescription holds information needed for constructing a valid ISC $SYSTEM.OBJ.ImportDir command
type ImportDescription struct {
Dir string
FilePattern string
Recursive bool
Qualifiers string
}
// NewImportDescription creates and returns a new import description based on the provided glob pattern and ISC qualifiers
func NewImportDescription(pathGlob string, qualifiers string) (*ImportDescription, error) {
glob := &ImportDescription{Qualifiers: qualifiers}
s := strings.Split(pathGlob, "**")
switch len(s) {
case 1:
glob.Dir = filepath.Dir(s[0])
glob.FilePattern = filepath.Base(s[0])
case 2:
glob.Dir = filepath.Clean(s[0])
if !strings.HasPrefix(s[1], "/") {
return nil, ErrMissingPathSeparator
}
if filepath.Dir(s[1]) != "/" {
return nil, ErrPathAfterRecursiveDirs
}
glob.FilePattern = filepath.Base(s[1])
glob.Recursive = true
default:
return nil, ErrTooManyRecursiveDirs
}
if strings.Contains(glob.Dir, "*") {
return nil, ErrWildcardInDirectory
}
if glob.Dir == "." {
if cwd, err := os.Getwd(); err == nil {
glob.Dir = cwd
} else {
return nil, err
}
}
return glob, nil
}
// String returns an ISC $SYSTEM.OBJ.ImportDir command as a string
func (i *ImportDescription) String() string {
var rec uint16
if i.Recursive {
rec = 1
} else {
rec = 0
}
return fmt.Sprintf(importFmtStr, i.Dir, i.FilePattern, i.Qualifiers, rec)
}