Skip to content

Commit 4c2d3fb

Browse files
authored
Merge pull request #11 from giusdp/main
Add -opspath tool
2 parents 01bd374 + 69ae3f8 commit 4c2d3fb

4 files changed

Lines changed: 141 additions & 1 deletion

File tree

tests/opspath.bats

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
setup() {
19+
load 'test_helper/bats-support/load'
20+
load 'test_helper/bats-assert/load'
21+
export NO_COLOR=1
22+
}
23+
24+
@test "-opspath help message" {
25+
run ops -opspath
26+
assert_line "Usage:"
27+
28+
run ops -opspath -h
29+
assert_line "Usage:"
30+
}
31+
32+
@test "-opspath with relative path" {
33+
WD=$(pwd)
34+
run ops -opspath ./test
35+
assert_success
36+
assert_output "${WD}/test"
37+
}
38+
39+
@test "-opspath with absolute path" {
40+
WD=$(pwd)
41+
run ops -opspath "${WD}/test"
42+
assert_success
43+
assert_output "${WD}/test"
44+
45+
run ops -opspath "${WD}/./test"
46+
assert_success
47+
assert_output "${WD}/test"
48+
}

tools/opspath.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package tools
2+
3+
import (
4+
"errors"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/mitchellh/go-homedir"
11+
)
12+
13+
func OpsPath() (int, error) {
14+
flags := flag.NewFlagSet("opspath", flag.ExitOnError)
15+
flags.Usage = func() {
16+
fmt.Println(MarkdownHelp("opspath"))
17+
}
18+
19+
showHelp := flags.Bool("h", false, "Show help")
20+
21+
// Parse command-line arguments
22+
if err := flags.Parse(os.Args[1:]); err != nil {
23+
return 1, err
24+
}
25+
26+
if *showHelp {
27+
flags.Usage()
28+
return 0, nil
29+
}
30+
31+
if flags.NArg() != 1 {
32+
flags.Usage()
33+
return 1, errors.New("error: no path provided")
34+
}
35+
36+
inputPath := filepath.Clean(filepath.FromSlash(flags.Arg(0))) // ensure correct OS separator
37+
38+
if filepath.IsAbs(inputPath) {
39+
fmt.Println(inputPath)
40+
return 0, nil
41+
}
42+
43+
expandedPath, err := homedir.Expand(inputPath)
44+
if err != nil {
45+
return 1, err
46+
}
47+
48+
if filepath.IsAbs(expandedPath) {
49+
fmt.Println(expandedPath)
50+
return 0, nil
51+
}
52+
53+
fullPath, err := filepath.Abs(filepath.Join(os.Getenv("OPS_PWD"), expandedPath))
54+
if err != nil {
55+
return 1, err
56+
}
57+
58+
fmt.Println(fullPath)
59+
60+
return 0, nil
61+
}

tools/opspath.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Join a relative path to the path from where `ops` was executed.
2+
This command is useful when creating custom tasks ( e.g. an ops plugin).
3+
4+
```text
5+
Usage:
6+
ops -opspath <path>
7+
```
8+
9+
Options:
10+
11+
```
12+
-h, --help print this help info
13+
```
14+
15+
## Examples
16+
17+
### You are executing in directory `/home/user/my/custom/dir`
18+
19+
```bash
20+
ops -opspath my-file.txt
21+
```
22+
23+
This will output:
24+
25+
```text
26+
/home/user/my/custom/dir/my-file.txt
27+
```

tools/tools.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ var ToolList = []Tool{
9595
{"executable", true}, // @DOC
9696
{"empty", true}, // @DOC
9797
{"extract", true}, // @DOC
98+
{"opspath", true}, // @DOC
9899
}
99100

100101
func IsTool(name string) bool {
@@ -230,6 +231,9 @@ func RunTool(name string, args []string) (int, error) {
230231
case "empty":
231232
os.Args = append([]string{"empty"}, args...)
232233
return Empty()
234+
case "opspath":
235+
os.Args = append([]string{"opspath"}, args...)
236+
return OpsPath()
233237

234238
default:
235239
return 1, fmt.Errorf("unknown tool")
@@ -238,7 +242,7 @@ func RunTool(name string, args []string) (int, error) {
238242
}
239243

240244
func MergeToolsList(mainTools []string) []string {
241-
availableTools := append(mainTools)
245+
availableTools := mainTools
242246
for _, tool := range ToolList {
243247
availableTools = append(availableTools, tool.Name)
244248
}

0 commit comments

Comments
 (0)