~emersion/hut-dev

hut init: move file/folder opening earlier v1 SUPERSEDED

Jackson Chen: 1
 hut init: move file/folder opening earlier

 1 files changed, 12 insertions(+), 12 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/hut-dev/patches/44265/mbox | git am -3
Learn more about email & git

[PATCH] hut init: move file/folder opening earlier Export this patch

So that hut init doesn't complain too late, which is when the token has
been verified to be functional.
---
I found out that hut init wouldn't save the token I gave it, because a
config file already exists. It complained way too late, and I think it
should warn earlier.

Could be better handled by replacing the token, because the tokens expire
after a year anyways. Although that might be better as a separate command,
or just left as manual config editing.

 config.go | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/config.go b/config.go
index 51023ac..693e439 100644
--- a/config.go
+++ b/config.go
@@ -177,6 +177,18 @@ func newInitCommand() *cobra.Command {
			instance = "sr.ht"
		}

		if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil {
			log.Fatalf("failed to create config file parent directory: %v", err)
		}

		f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
		if os.IsExist(err) {
			log.Fatalf("config file %q already exists (delete it if you want to overwrite it)", filename)
		} else if err != nil {
			log.Fatalf("failed to create config file: %v", err)
		}
		defer f.Close()

		baseURL := "https://meta." + instance
		fmt.Printf("Generate a new OAuth2 access token at:\n")
		fmt.Printf("%s/oauth2/personal-token\n", baseURL)
@@ -199,18 +211,6 @@ func newInitCommand() *cobra.Command {
			log.Fatalf("failed to check OAuth2 token: %v", err)
		}

		if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil {
			log.Fatalf("failed to create config file parent directory: %v", err)
		}

		f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
		if os.IsExist(err) {
			log.Fatalf("config file %q already exists (delete it if you want to overwrite it)", filename)
		} else if err != nil {
			log.Fatalf("failed to create config file: %v", err)
		}
		defer f.Close()

		if _, err := f.WriteString(config); err != nil {
			log.Fatalf("failed to write config file: %v", err)
		}
-- 
2.42.0
Hm, this will create an empty file on failure I believe, which is not
great… Maybe we can add an early os.Stat check instead?