Skip to content

Commit

Permalink
Create sample config file if none exists
Browse files Browse the repository at this point in the history
  • Loading branch information
spurll committed Jan 17, 2019
1 parent cf8b3a5 commit d3eceb5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -17,9 +17,9 @@ where `config` is the (optional) path to a JSON configuration file, defaulting t

* Python 3.6+

## Setup
## Configuration

Prior to running, you must create a JSON configuration file that specifies the processes
Prior to running, you should create a JSON configuration file that specifies the processes
to be managed, and optionally the directory in which to place the logs:

```json
Expand All @@ -44,6 +44,8 @@ to be managed, and optionally the directory in which to place the logs:
}
```

If tsk cannot find a configuration file at startup, it will create an example file.

### Process Fields

* `name`: The name tsk will use to refer to this process
Expand Down
49 changes: 43 additions & 6 deletions tsk.py
Expand Up @@ -12,13 +12,13 @@


processes = []
log_dir = os.path.join('~', '.tsk.logs')
log_dir = os.path.join('~', '.tsk.log')
log_archive = 5
config_file = ''


def main():
with open(config_file, 'r') as f:
config = json.load(f)
config = load_config()

# Prepare log directory
global log_dir
Expand All @@ -29,7 +29,8 @@ def main():

try:
# Create process objects
log_archive = config.get('log-archive', 5)
global log_archive
log_archive = config.get('log-archive', log_archive)
processes.extend(
Process(**p, log_dir=log_dir, log_archive=log_archive)
for p in config.get('processes', {})
Expand Down Expand Up @@ -129,13 +130,49 @@ def select(selection):
return False


def load_config():
if not os.path.exists(config_file):
create_config()

with open(config_file, 'r') as f:
return json.load(f)


def create_config():
# Ensure the parent directories exist
os.makedirs(os.path.dirname(config_file), exist_ok=True)

# Create a dummy config
config = {
'logs': log_dir,
'log-archive': log_archive,
'processes': [
{
'name': 'Text Editor',
'cmd': 'notepad.exe' if platform.system() == 'Windows'
else 'textedit' if platform.system() == 'Darwin'
else 'gedit'
},
{
'name': 'List Home Directory',
'cmd': 'dir' if platform.system() == 'Windows' else 'ls -l',
'cwd': '~'
}
]
}

# Write dummy config to config file
with open(config_file, 'w') as f:
json.dump(config, f, indent=4)


if __name__ == '__main__':
default_config = os.path.expanduser(os.path.join('~', '.tsk.json'))
default_config = os.path.join('~', '.tsk.json')

parser = argparse.ArgumentParser()
parser.add_argument('config', nargs='?', default=default_config)
args = parser.parse_args()

config_file = args.config
config_file = os.path.abspath(os.path.expanduser(args.config))

main()

0 comments on commit d3eceb5

Please sign in to comment.