-
Notifications
You must be signed in to change notification settings - Fork 7
/
3.input-from-shell.main.kts
executable file
·72 lines (58 loc) · 2.29 KB
/
3.input-from-shell.main.kts
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
#!/usr/bin/env kotlin
@file:Repository("https://jitpack.io")
@file:DependsOn("com.github.kaushikgopal:shell.main.kts:276950a346")
import sh.kau.shell.ShellConsole.Companion.ANSI_GRAY
import sh.kau.shell.ShellConsole.Companion.ANSI_GREEN
import sh.kau.shell.ShellConsole.Companion.ANSI_PURPLE
import sh.kau.shell.ShellConsole.Companion.ANSI_RESET
import sh.kau.shell.ShellConsole.Companion.ANSI_YELLOW
import sh.kau.shell.runInShell
/*
* This script demonstrates how you can take inputs from the user
* through an interactive way by prompting users for inputs step by step.
*
* There's a few concepts being demonstrated here:
* 1. how to print things in color
* 2. how to take user inputs from the command line
* 3. how to execute shell commands from within a kotlin script
*
* To run kscripts do it like so:
$> brew install kotlin
$> kotlin 3.input-from-shell.main.kts
*
* Alternatively, make this script executable
$> chmod +x <script-name>.main.kts
$> ./<script-name>.main.kts <args>
*
* Read more about this at https://kau.sh/blog/kscript
*/
println("$ANSI_GRAY ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ PROGRAM START$ANSI_RESET")
program(args)
println("$ANSI_GRAY ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ PROGRAM END $ANSI_RESET")
fun program(args: Array<String>) {
println("$ANSI_GRAY[args]$ANSI_GREEN${args.joinToString()}$ANSI_RESET")
// simple command - taking an input
// print it back out
print("\n\n$ANSI_PURPLE Enter your name: $ANSI_RESET")
val name = readlnOrNull()
println("\n\n$ANSI_GRAY Why hello $ANSI_YELLOW $name! $ANSI_RESET\n\n\n")
// take a domain name
// open social media links such as http://threads.<domain.name>
print("Do you have a domain name? (y/N): ")
val hasDomain = readlnOrNull()
var domain: String? = null
if (hasDomain == "y") {
print("What is your domain name: ")
domain = readlnOrNull()
}
domain = domain ?: "kau.sh"
println("${ANSI_GRAY}🚧 Opening all the social profiles now!$ANSI_RESET")
// execute a shell command
listOf(
"open https://threads.$domain",
"open https://github.$domain",
"open https://twitter.$domain",
"open https://mastodon.$domain",
"open https://instagram.$domain"
).forEach { it.runInShell() }
}