Skip to content

Commit 5ad0493

Browse files
authored
v0.2.0: Config file feature fuctionality
0.2.0: Config file
2 parents 2f5a305 + 67aa574 commit 5ad0493

17 files changed

Lines changed: 464 additions & 107 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ bitburner/
77
build/
88
.vscode/
99
.nvim
10+
*.toml
1011

1112
*.exe

Makefile

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,68 @@ CURRENT_VERSION_MINOR := $(MAJOR).$(NEXT_MINOR).0
3030
CURRENT_VERSION_MAJOR := $(NEXT_MAJOR).0.0
3131
endif
3232

33+
.DEFAULT_GOAL := help
3334
# --- Version commands ---
34-
.PHONY: version
35-
36-
version:
35+
.PHONY: help version proto
36+
37+
help: ##@help Display all commands and descriptions
38+
@awk 'BEGIN {FS = ":.*##@"; printf "\nUsage:\n make <target>\n"} \
39+
/^[.a-zA-Z_-]+:.*?##@/ { \
40+
split($$2, parts, " "); \
41+
section = parts[1]; \
42+
description = substr($$2, length(section) + 2); \
43+
sections[section] = sections[section] sprintf(" \033[36m%-15s\033[0m %s\n", $$1, description); \
44+
} \
45+
END { \
46+
for (section in sections) { \
47+
printf "\n\033[1m%s\033[0m\n", section; \
48+
printf "%s", sections[section]; \
49+
} \
50+
}' $(MAKEFILE_LIST)
51+
52+
version: ##@help Log the current version
3753
@echo "v$(CURRENT_VERSION_PATCH)"
3854

55+
# proto: ##@help For prototyping makefile functionality
56+
# @echo hello "$@"
57+
3958
# -- Project --
40-
.PHONY: run wrun build-win build-linux build
59+
.PHONY: run wrun debug test build-win build-linux build
60+
61+
run: ##@run Run normally. Pass arguments like so: args="arg1 arg2 ...".
62+
go run ./main.go $(args)
63+
64+
wrun: ##@run Run and watch for file changes. Requires wgo: https://github.com/bokwoon95/wgo
65+
wgo run ./main.go $(args)
4166

42-
run:
43-
go run ./main.go
67+
debug: ##@run Run and watch with the --test flag. Requires wgo: https://github.com/bokwoon95/wgo
68+
wgo run . $(args) --test
4469

45-
wrun: # requires wgo: https://github.com/bokwoon95/wgo
46-
wgo run ./main.go
70+
test: ##@run go test and watch. Requires wgo: https://github.com/bokwoon95/wgo
71+
wgo -file .go go test -v ./...
4772

48-
build-win:
49-
GOOS=windows GOARCH=386 go build -o ./build/BitburnerGoFilesync.exe ./main.go
73+
build-win: ##@build Build for windows. Binary will be located at ./build/
74+
GOOS=windows GOARCH=amd64 go build -o ./build/BitburnerGoFilesync.exe ./main.go
5075

51-
build-linux:
76+
build-linux: ##@build Build for linux. Binary will be located at ./build/
5277
GOOS=linux GOARCH=amd64 go build -o ./build/BitburnerGoFilesync ./main.go
5378

54-
build:
79+
build: ##@build Build for both windows and linux. Binary will be located at ./build/
5580
$(MAKE) build-win
5681
$(MAKE) build-linux
5782

5883
# -- Release --
5984
.PHONY: version-update patch minor major
6085

61-
version-update:
86+
version-update:
6287
git tag "v$(VERS)"
6388
git push --tags
6489

65-
patch:
90+
patch: ##@versioning Release a patch (vx.x.+commits)
6691
$(MAKE) version-update VERS=$(MAJOR).$(MINOR).$(NEXT_PATCH)
6792

68-
minor:
93+
minor: ##@versioning Release a minor (vx.+1.x)
6994
$(MAKE) version-update VERS=$(MAJOR).$(NEXT_MINOR).0
7095

71-
major:
96+
major: ##@versioning Release a major (v+1.x.x)
7297
$(MAKE) version-update VERS=$(NEXT_MAJOR).0.0

README.md

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,58 @@ Current automated triggers:
3030
- linux: `BitburnerGoFilesync`
3131
2. Put the executable in same directory where you keep all your bitburner scripts
3232
3. Open a commandline in the directory you put the executable in
33-
4. Move on to **Usage**
33+
4. Move on to [Usage](#usage)
3434

35-
You may skip step 2 and put the executable anywhere you like, but for the program to work you will then have to supply it with a `--dir` argument flag, more about that in **Arguments**.
35+
You may skip step 2 and put the executable anywhere you like, but for the program target the correct files,
36+
<br>you may need to specify youre preferred file path in the [config](#config),
37+
<br>or pass in the `--dir` argument, more about that in [Arguments](#arguments).
3638

37-
Alternatively if you rather not download an executable from this repository (I would not blame you for being careful) you may also clone this repository and build it yourself:
39+
Alternatively if you rather not download an executable from this repository (I would not blame you for being careful)
40+
you may also clone this repository and [run/build it yourself](#how-to-build).
3841

39-
## How to build
42+
## Usage
4043

41-
### Requirements
42-
- [golang](https://go.dev/doc/install): To be able to run the code
44+
Once the executable is installed you are ready to get started with the game, the only thing you would still need to do is start the filesyncer via the commandline that you should have opened in step 3 of the installation guide.
4345

44-
Once you have a clone of this repository and you installed the *long list* of requirements you may run the project in any of the following ways:
45-
- `make run`: This will run the project without any arguments.<br>
46-
This method __does not__ accept arguments.
47-
- `go run main.go`: This is what `make run` would have done for you.<br>
48-
This method __does accept__ arguments.
49-
- `make build`: This will compile the project into a binary.<br>
50-
The binary will be located relative to the working directory of this project (where the main.go is) at `./build/`.
46+
Simply enter `BitburnerGoFilesync.exe` (or whatever the executable is called on your system) and pass in any arguments that you like to run it with.
5147

52-
For more run methods you can check out the `./Makefile` and try any of its commands under `-- Project --`.
48+
## Config
5349

50+
A config file will be created for you once you run the tool for the first time.
5451

55-
## Usage
52+
The file will be located in the same directory that you ran the tool in, you can change that with the `--config` argument (use `--help config` for more info).
5653

57-
Once the executable is installed you are ready to get started with the game, the only thing you would still need to do is start the filesyncer via the commandline that you should have opened in step 3 of the installation guide.
54+
The config file by default will be named `config.toml` and the initial content will be all possible fields for the config with their default values assigned to them.
55+
56+
### Config Fields
5857

59-
Simply enter `BitburnerGoFilesync.exe` (or whatever the executable is called on your system) and pass in any arguments that you like.
58+
**Legend**:
59+
- `[field]`: Group Section Header. This row's default column will instead be empty or hold relevant info.
60+
- `- field`: Section child field.
6061

62+
| Field | Description | Default |
63+
|:-----|:------------|:-------:|
64+
| Port | Set the port for the server to connect to. | `"8080"` |
65+
| Directory | Specify the directory where this tool should watch for file changes to sync up with bitburner. | `"./"` |
66+
| FileScanInterval | The amount of miliseconds the file scanner waits each loop. | `100` |
67+
| [FilePatterns] | Holds include and exclude file pattern matching fields which allow you to define which files to sync and which not to. | [Pattern matching rules](https://github.com/bmatcuk/doublestar?tab=readme-ov-file#patterns) |
68+
| - Include | Which files should be included. | `["**/*.js", "**/*.ts"]` |
69+
| - Exclude | Which files to ignore. This is checked before the include patterns. | `["**/*.d.ts"]` |
70+
71+
## How to build
72+
73+
### Requirements
74+
- [golang](https://go.dev/doc/install): To be able to run the project.
75+
- (Optional) [wgo](https://github.com/bokwoon95/wgo): This is required for some Makefile targets.
76+
<br>It is a tool that also watches the project for file changes and restarts it once anything is detected.
77+
78+
Once you have a clone of this repository and you installed the "*long list*" of requirements you can run the project in one of two ways:
79+
1. Use `make [target]`.
80+
<br>To see all possible targets and their descriptions, run `make help`.
81+
2. Manually type out what you want to run.
82+
<br>This would be something along the lines of `go run . --arg1 param --arg2 ...`.
83+
84+
If you are looking to contribute, please read the [Contribution Guidelines](https://github.com/CTNOriginals/BitburnerGoFilesync?tab=readme-ov-file).
6185

6286
## Arguments
6387

@@ -67,11 +91,11 @@ Formatting Rules:
6791
Each new argument always has to start with a double dash '--'.
6892
If the argument does not start with '--' it is considered a parameter
6993
for the most recent argument that started with '--'.
70-
94+
7195
Each argument may have any number of parameters,
7296
to check what an argument may accept or require,
7397
you can do --help followed by the name of the argument without the '--'.
74-
98+
7599
Some arguments may accept a specific amount of parameters where others accept a range.
76100
If an argument doesnt have its required parameters, it will say so in the console,
77101
this argument will not execute anything after that and will be ignored.
@@ -90,6 +114,15 @@ Formatting Rules:
90114
The same as --help, but it also includes all of the extra information
91115
as if you entered --help <command> for each argument.
92116
117+
--config:
118+
Define the config.toml file path.
119+
By default, the config file is located in the same directory as the binary.
120+
If no config file exists at the specified location, one will be created.
121+
Parameters:
122+
filepath:
123+
The path to the config file
124+
Default: /home/ctn/code/bitburner/gofilesync/config.toml
125+
93126
--dir:
94127
Specify the directory where this tool should watch
95128
for file changes to sync up with bitburner
@@ -128,7 +161,7 @@ Formatting Rules:
128161
129162
DEBUG ARGUMENTS
130163
131-
--test:
164+
--test, --debug:
132165
Runs the test function if it exists
133166
134167
--no-watcher:

arguments/arguments.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/CTNOriginals/BitburnerGoFilesync/communication"
1010
"github.com/CTNOriginals/BitburnerGoFilesync/communication/constructor"
1111
"github.com/CTNOriginals/BitburnerGoFilesync/communication/definitions"
12+
"github.com/CTNOriginals/BitburnerGoFilesync/config"
1213
"github.com/CTNOriginals/BitburnerGoFilesync/constants"
1314
"github.com/CTNOriginals/BitburnerGoFilesync/test"
1415
"github.com/gorilla/websocket"
@@ -107,6 +108,27 @@ var argumentList = argList{
107108
os.Exit(0)
108109
},
109110
},
111+
{Alias: []string{"--config"},
112+
Description: []string{
113+
"Define the config.toml file path.",
114+
"By default, the config file is located in the same directory as the binary.",
115+
"If no config file exists at the specified location, one will be created.",
116+
},
117+
Params: argParameters{
118+
{Name: "filepath", Description: []string{
119+
"The path to the config file",
120+
"Default: " + constants.ConfigFilePath,
121+
}},
122+
},
123+
Action: func(params []string) {
124+
if len(params) == 0 {
125+
fmt.Print("'--config' requires at least 1 parameter.\n")
126+
os.Exit(1)
127+
}
128+
129+
constants.ConfigFilePath = params[0]
130+
},
131+
},
110132
{Alias: []string{"--dir"},
111133
Description: []string{
112134
"Specify the directory where this tool should watch",
@@ -129,7 +151,7 @@ var argumentList = argList{
129151
os.Exit(1)
130152
}
131153

132-
constants.SetBitburnerDir(params[0])
154+
config.ValidateBitburnerDirectory(params[0])
133155
},
134156
},
135157
{Alias: []string{"--include-ext", "--ext"},
@@ -144,7 +166,7 @@ var argumentList = argList{
144166
}},
145167
},
146168
Action: func(params []string) {
147-
constants.IncludeFileExt = params
169+
config.Values.FilePatterns.Include = params
148170
},
149171
},
150172
{Alias: []string{"--port"},
@@ -164,7 +186,7 @@ var argumentList = argList{
164186
os.Exit(1)
165187
}
166188

167-
constants.Port = params[0]
189+
config.Values.Port = params[0]
168190
},
169191
},
170192
{Alias: []string{"--scan-interval", "--interval"},
@@ -196,7 +218,7 @@ var argumentList = argList{
196218
os.Exit(1)
197219
}
198220

199-
constants.FileScanDelay = int(num)
221+
config.Values.FileScanInterval = int(num)
200222
},
201223
},
202224
{Alias: []string{"--get-definitions"},
@@ -217,7 +239,7 @@ var argumentList = argList{
217239
fmt.Printf("'--get-definitions' expects a string response but received another type instead: %v", message.Response)
218240
return
219241
}
220-
ctnfile.WriteFile(constants.BitburnerRoot+"/NetscriptDefinitions.d.ts", strings.Split(content, "\n"))
242+
ctnfile.WriteFile(config.Values.Directory+"/NetscriptDefinitions.d.ts", strings.Split(content, "\n"))
221243
}
222244

223245
var onConnect = func(ws *websocket.Conn) {
@@ -230,12 +252,13 @@ var argumentList = argList{
230252

231253
{Alias: []string{"DEBUG ARGUMENTS"}},
232254

233-
{Alias: []string{"--test"},
255+
{Alias: []string{"--test", "--debug"},
234256
Description: []string{
235257
"Runs the test function if it exists",
236258
},
237259
Params: argParameters{},
238260
Action: func(params []string) {
261+
constants.Debug = true
239262
test.DoTest()
240263
os.Exit(0)
241264
},

arguments/parser.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
11
package arguments
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"slices"
6+
)
47

58
type argStream struct {
69
Alias string
710
Params []string
811
}
912

13+
// Parse specific args removes any args and their params
14+
// from args if they are not included in list
15+
// or if include is false, the args in list will be remove from args instead.
16+
func ParseSpecificArgs(args []string, include bool, list ...string) {
17+
if len(list) == 0 {
18+
if !include {
19+
ParseArgs(args)
20+
}
21+
22+
return
23+
}
24+
25+
var validArgs = []string{}
26+
var validArg = false
27+
28+
for _, part := range args {
29+
if len(part) >= 2 && part[0:2] == "--" {
30+
validArg = include == slices.Contains(list, part)
31+
}
32+
33+
if validArg {
34+
validArgs = append(validArgs, part)
35+
}
36+
}
37+
38+
ParseArgs(validArgs)
39+
}
40+
1041
func ParseArgs(args []string) {
1142
onInitList = argumentList
1243

0 commit comments

Comments
 (0)