aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 3dfe15c2b0a62d8ec197e92e0b599345871c8633 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package main

import (
	"errors"
	"fmt"
	"log"
	"os"
	"path"
	"path/filepath"
	"sort"
	"strings"
	"time"
)

/*********************************************
 * Workspace Root
 *********************************************/

type workspaceRoot struct {
	path string
};

func systemWorkspaceRoot() workspaceRoot {
	const WORKSPACE_DIR = "ws"

	home, err := os.UserHomeDir()

	if err != nil {
		log.Fatalf("The user home directory is not defined: %v", err)
	}

	workspaceRootPath := filepath.Join(home, WORKSPACE_DIR);

	_, err = os.Stat(workspaceRootPath)

	if os.IsNotExist(err) {
		if err := os.Mkdir(workspaceRootPath, 0755); err != nil {
			log.Fatalf("Failed to create directory: %s", workspaceRootPath);
		}
	}

	return workspaceRoot { workspaceRootPath }
}

type workspaceDirs []*workspaceDir

func (wsRoot *workspaceRoot) readWorkspaces() workspaceDirs {
	dirs, err := os.ReadDir(wsRoot.path)

	if err != nil {
		log.Fatalf("Failed to read workspaces in %s: %v", wsRoot.path, err);
	}

	workspaces := make([]*workspaceDir, 0)

	for _, dir := range dirs {
		if !dir.Type().IsDir() {
			continue
		}

		wsDir, err := newWorkspaceFromName(wsRoot, dir.Name())

		if err != nil {
			log.Printf("Error reading directory in workspace %s: %s: %v", wsRoot.path, dir.Name(), err);
			continue
		}

		workspaces = append(workspaces, wsDir);
	}

	return workspaces
}

func (dirs workspaceDirs) findDirByName(name string) *workspaceDir {
	birthTime, nonce, err := parseWorkspaceName(name)

	if err != nil {
		log.Fatalf("Cannot find directory %s, error parsing name", name);
	}

	var dir *workspaceDir = nil

	for _, dir = range dirs {
		if dir.birthTime.Equal(*birthTime) && dir.nonce == *nonce {
			break
		}
	}

	return dir
}

/*********************************************
 * Workspace Directory Object
 *********************************************/

type workspaceDir struct {
	birthTime time.Time
	nonce string
	accessTime time.Time
	parent *workspaceRoot
}

var MisformattedWorkspaceError = errors.New("Misformatted workspace name error")

func formatWorkspaceName(birthTime time.Time, nonce string) string {
	dateTime := birthTime.UTC().Format(time.DateTime)
	dateTime = strings.ReplaceAll(dateTime, " ", "_")
	return dateTime + "." + nonce
}

func parseWorkspaceName(name string) (birthTime *time.Time, nonce *string, err error) {
	parts := strings.Split(name, ".")

	if len(parts) != 2 {
		return nil, nil, MisformattedWorkspaceError
	}

	dateTime := strings.ReplaceAll(parts[0], "_", " ")

	timePart, err := time.ParseInLocation(time.DateTime, dateTime, time.UTC)
	noncePart := parts[1]

	if err != nil {
		return nil, nil, MisformattedWorkspaceError
	}

	return &timePart, &noncePart, nil
}

func newWorkspaceFromName(parent *workspaceRoot, name string) (dir *workspaceDir, err error) {
	dir = nil

	birthTime, nonce, err := parseWorkspaceName(name)
	if err != nil {
		return
	}

	st, err := os.Stat(path.Join(parent.path, name))
	if err != nil {
		return
	}

	dir = &workspaceDir {
		*birthTime,
		*nonce,
		st.ModTime(),
		parent,
	}

	return
}

func createWorkspaceDir(ws *workspaceRoot) workspaceDir {
	birthTime := time.Now()
	namePattern := formatWorkspaceName(birthTime, "*")

	dir, err := os.MkdirTemp(ws.path, namePattern)
	if err != nil {
		log.Fatalf("Failed to create new workspace directory: %v", err);
	}

	dir = path.Base(dir)

	_, nonce, err := parseWorkspaceName(dir)

	if err != nil {
		log.Fatalf("Failed to parse the workspace name: %v\n", err);
	}

	return workspaceDir {
		birthTime,
		*nonce,
		birthTime,
		ws,
	};
}

func (dir *workspaceDir) name() string {
	return formatWorkspaceName(dir.birthTime, dir.nonce)
}

func (dir *workspaceDir) path() string {
	return path.Join(dir.parent.path, dir.name())
}

func (dir *workspaceDir) timeSinceLastAccess() time.Duration {
	return time.Since(dir.accessTime)
}

/*********************************************
 * Commands Helpers
 *********************************************/

const (
	secondsInSec = 1
	minsInSec = 60 * secondsInSec
	hourInSec = 60 * minsInSec
	dayInSec = 24 * hourInSec
)

func formatTimestamp(duration time.Duration) string {
	secs := int64(duration.Seconds())
	days := secs / dayInSec
	secs %= dayInSec
	hours := secs / hourInSec
	secs %= hourInSec
	mins := secs / minsInSec
	secs %= minsInSec
	
	return fmt.Sprintf("%d days %d hours %d min %d secs", days, hours, mins, secs)
}

func readRecencySortedWorkspaces() (*workspaceRoot, workspaceDirs) {
	wsRoot := systemWorkspaceRoot()
	workspaces := wsRoot.readWorkspaces()

	sort.Slice(workspaces, func(i, j int) bool {
		return workspaces[i].accessTime.Compare(workspaces[j].accessTime) < 0
	})

	return &wsRoot, workspaces
}

func currentWorkspace(wsRoot *workspaceRoot, workspaces workspaceDirs) int {

	cwd, err := os.Getwd()

	if err != nil {
		log.Fatalf("Failed to obtain the working directory: %s\n", err);
	}

	if !strings.HasPrefix(cwd, wsRoot.path) {
		log.Fatalf("The current working directory is not in a workspace\n")
	}

	dirName := path.Base(cwd)

	cur_idx := 0
	for ; cur_idx < len(workspaces); cur_idx++ {
		dir := workspaces[cur_idx]
		if dir.name() == dirName {
			break
		}
	}

	if cur_idx == len(workspaces) {
		log.Fatalf("Failed to find %s\n", dirName)
	}

	return cur_idx
}

/*********************************************
 * Commands
 *********************************************/

func createNewWorkspace() {
	ws := systemWorkspaceRoot()
	workspace := createWorkspaceDir(&ws)
	path := workspace.path()

	if err := os.Chdir(path); err != nil {
		log.Fatalf("Failed to change into directory: %v\n", err)
	}

	fmt.Println(path)
}

func listWorkspaces() {
	_, workspaces := readRecencySortedWorkspaces()

	for i, ws := range workspaces {
		delta := ws.timeSinceLastAccess()

		fmt.Println(i + 1, ws.name(), formatTimestamp(delta))
	}
}

func printRecentWorkspace() {
	_, workspaces := readRecencySortedWorkspaces()

	if len(workspaces) == 0 {
		log.Fatalln("There are currently no workspaces. Create a workspace with `ws new`.")
	}

	fmt.Println(workspaces[len(workspaces) - 1].path())
}

type printWorkspaceSubcmd int

const (
	PrevWs printWorkspaceSubcmd = iota
	NextWs
	CurrWs
)

func printWorkspace(subcmd printWorkspaceSubcmd) {
	wsRoot, workspaces := readRecencySortedWorkspaces()

	idx := currentWorkspace(wsRoot, workspaces)

	switch subcmd {
	case PrevWs:
		if idx <= 0 {
			log.Fatalln("The first workspace has been reached")
		}

		idx--
	case NextWs:
		if idx + 1 >= len(workspaces) {
			log.Fatalln("The current workspace is the newest workspace")
		}

		idx++
	}

	fmt.Println(workspaces[idx].path())
}

func printWorkspaceRoot() {
	wsRoot := systemWorkspaceRoot()
	fmt.Println(wsRoot.path)
}

func fatalPrintUsage() {
fmt.Printf(`
	ws internal

	ws_internal create_new_workspace
	ws_internal list_workspaces
	ws_internal print_recent_workspace
	ws_internal print_current_workspace
	ws_internal print_next_workspace
	ws_internal print_prev_workspace
	ws_internal print_workspace_root
	ws_internal activate
`)
	os.Exit(1)
}

func main() {

	if len(os.Args) < 1 {
		os.Exit(127)
	}

	prog := os.Args[0]

	log.SetPrefix(prog + ": ")
	log.SetFlags(0)

	if len(os.Args) < 2 {
		fatalPrintUsage()
	}

	cmd := os.Args[1]

	switch cmd {
	case "create_new_workspace":
		createNewWorkspace()
	case "list_workspaces":
		listWorkspaces()
	case "print_recent_workspace":
		printRecentWorkspace()
	case "print_current_workspace":
		printWorkspace(CurrWs)
	case "print_next_workspace":
		printWorkspace(NextWs)
	case "print_prev_workspace":
		printWorkspace(PrevWs)
	case "print_workspace_root":
		printWorkspaceRoot()
	case "activate":
		fmt.Print(shellWrapper)
	default:
		fatalPrintUsage()
	}
	
}