-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathnotionterm.go
executable file
Β·189 lines (159 loc) Β· 5.8 KB
/
notionterm.go
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
package main
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/ariary/go-utils/pkg/host"
"github.com/ariary/notionterm/pkg/notionterm"
"github.com/jomei/notionapi"
"github.com/spf13/cobra"
)
var PageUrl, Token string
func main() {
var delay int
var isServerMode, isConfigFromPage bool
var config notionterm.Config
//CMD ROOT
var rootCmd = &cobra.Command{Use: "notionterm", Run: func(cmd *cobra.Command, args []string) {
// Initialization
Init(&config, isServerMode)
DetectExternalIP(&config)
if config.ExternalIP == "" {
fmt.Println("β Failed to get external URL/IP from flags and detection")
os.Exit(92)
}
if config.ExternalUrl == "" {
config.ExternalUrl = "https://" + config.ExternalIP + ":" + config.Port
} else {
config.ExternalUrl = "https://" + config.ExternalUrl
}
if isServerMode {
config.PageID = notionterm.LaunchUrlWaitingServer(&config)
notionterm.DeleteEmbed(config)
}
var play = make(chan struct{})
var pause = make(chan struct{})
stopNotion := notionterm.LaunchNotiontermServer(&config, isServerMode, play, pause)
// creation
config.CaptionBlock.Id = notionterm.CreateButtonBlock(config)
config.CaptionBlock.Type = notionapi.BlockTypeEmbed
if _, err := notionterm.UpdateCaptionById(config.Client, config.PageID, config.CaptionBlock, config.Path); err != nil { //add caption
fmt.Println("Failed setting button caption:", err)
}
config.TerminalBlockId = notionterm.CreateTerminalBlock(config)
// run
go notionterm.NotiontermRun(&config, play, pause)
pause <- struct{}{}
<-stopNotion
}}
//CMD OUTGOING/LIGHT
var cmdOutgoing = &cobra.Command{ //only outgoing HTTP traffic
Use: "light",
Short: "only grab information from notion page. No HTTP ingoing traffic is used to work.",
Long: `only grab information from notion page by performing outgoing HTTP request. No HTTP ingoing traffic is allowed/required so the notionterm is not used as a server.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
// Initialization
Init(&config, isServerMode)
var play = make(chan struct{})
var pause = make(chan struct{})
stopNotion := make(chan bool)
// creation
config.CaptionBlock.Id = notionterm.CreateHeadingCaptionBlock(config)
config.CaptionBlock.Type = notionapi.BlockTypeHeading3
config.TerminalBlockId = notionterm.CreateTerminalBlock(config)
if _, err := notionterm.UpdateCaptionById(config.Client, config.PageID, config.CaptionBlock, "π "+config.Path); err != nil { //add caption
fmt.Println("Failed setting button caption:", err)
}
go notionterm.NotiontermRun(&config, play, pause)
<-stopNotion
},
}
// FLAGS
//token,pageid,p,external,shell,delay,server,config-from-page
rootCmd.PersistentFlags().StringVarP(&Token, "token", "t", Token, "notion integration key")
rootCmd.PersistentFlags().StringVarP(&PageUrl, "page-url", "u", PageUrl, "notion page url")
rootCmd.PersistentFlags().StringVarP(&config.Port, "port", "p", "9292", "notionterm HTTP listening port")
rootCmd.PersistentFlags().StringVarP(&config.Shell, "shell", "r", "", "shell runtime to execute command with notionterm (sh, bash, and cmd.exe)")
rootCmd.PersistentFlags().IntVarP(&delay, "delay", "d", 500, "delay between each request to the notion page by notionterm")
rootCmd.Flags().StringVarP(&config.ExternalIP, "external", "e", "", "external URL/IP of the machine where notionterm runs")
rootCmd.Flags().StringVarP(&config.ExternalUrl, "override-url", "o", "", "override external url (useful if coupled with ngrok)")
rootCmd.Flags().BoolVarP(&isServerMode, "server", "s", false, "retrieve notion page id from ingoing HTTP request")
rootCmd.Flags().BoolVarP(&isConfigFromPage, "config-from-page", "c", false, "retrieve notionterm configuration from page")
rootCmd.AddCommand(cmdOutgoing)
rootCmd.Execute()
}
//Init: intialize additional variables (token, pageid, client, path, PS1 , shell runtime)
func Init(config *notionterm.Config, isServer bool) {
//integration token
if Token == "" {
Token = os.Getenv("NOTION_TOKEN")
if Token == "" {
fmt.Println("β Please set NOTION_TOKEN envvar with your integration token before launching notionterm or use --token flag")
os.Exit(92)
}
}
//pageid
if !isServer {
config.PageID = getPageId(PageUrl)
}
//client
config.Client = notionapi.NewClient(notionapi.Token(Token))
//get current path
if path, err := os.Getwd(); err != nil {
fmt.Println(err)
os.Exit(92)
} else {
config.Path = path
}
//PS1 & Shell runtime
switch runtime.GOOS {
case "linux":
config.PS1 = "$ "
if config.Shell == "" {
config.Shell = "sh"
}
case "windows":
config.PS1 = "> "
if config.Shell == "" {
config.Shell = "cmd.exe"
}
default:
config.PS1 = "$ "
}
}
//getPAgeId: return notion page id from a notion page url
func getPageId(pageurl string) (pageid string) {
if pageurl == "" {
pageurl = os.Getenv("NOTION_PAGE_URL")
if pageurl == "" {
fmt.Println("β Please set NOTION_PAGE_URL envvar with your page id before launching notionterm (CTRL+L on desktop app), or use --page flag")
os.Exit(92)
}
}
pageid = pageurl[strings.LastIndex(pageurl, "-")+1:]
if pageid == pageurl {
fmt.Println("β PAGEID was not found in NOTION_PAGE_URL. Ensure the url is in the form of https://notion.so/[pagename]-[pageid]")
os.Exit(92)
}
return pageid
}
//DetectExternalIP: Try to detect external IP using dig & host
func DetectExternalIP(config *notionterm.Config) {
// external ip/url
if config.ExternalIP == "" {
//try to find it
var ext string
ext, err := host.GetExternalIP()
config.ExternalIP = ext
if err != nil {
fmt.Println("Failed to detect external ip (dig):", err)
} else if config.ExternalIP == "" {
config.ExternalIP, err = host.GetHostIP()
if err != nil {
fmt.Println("Failed to detect external ip (hostname):", err)
}
}
}
}