/
input.go
45 lines (37 loc) · 985 Bytes
/
input.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
package base
import (
"fmt"
"os"
mysql "github.com/go-sql-driver/mysql"
)
type InputSource int
const (
UnknownInputSource InputSource = iota
StdInputSource
FileInputSource
DirectoryInputSource
UriInputSource
)
type ErrUnknownInputSource struct {
sourceVal string
}
func (e *ErrUnknownInputSource) Error() string {
return fmt.Sprintf("unknown input source: %s", e.sourceVal)
}
// DetectInputSource auto detects the type of source by the given value; it checks if the value
// is a file, directory, etc.
func DetectInputSource(inputSourceValue string) (InputSource, error) {
if inputSourceValue == "" {
return StdInputSource, nil
}
if fileInfo, err := os.Stat(inputSourceValue); err == nil {
if fileInfo.IsDir() {
return DirectoryInputSource, nil
}
return FileInputSource, nil
}
if _, err := mysql.ParseDSN(inputSourceValue); err == nil {
return UriInputSource, nil
}
return UnknownInputSource, &ErrUnknownInputSource{sourceVal: inputSourceValue}
}