From c770ab88db248d9511f06d1b104fc9c0248fce41 Mon Sep 17 00:00:00 2001 From: Mariano Uvalle Date: Wed, 24 Jan 2024 14:59:08 -0800 Subject: [PATCH 1/9] Increase memory --- fly.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fly.toml b/fly.toml index 8f81626..c35bd96 100644 --- a/fly.toml +++ b/fly.toml @@ -24,3 +24,7 @@ primary_region = "sea" auto_start_machines = true min_machines_running = 1 processes = ["app"] + +[vm] + size = "shared-cpu-1x" + memory = "1G" From f46461c886d171b4d5a2246e06b21453d2422e89 Mon Sep 17 00:00:00 2001 From: Mariano Uvalle Date: Wed, 24 Jan 2024 15:17:53 -0800 Subject: [PATCH 2/9] Update fly.toml --- fly.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fly.toml b/fly.toml index c35bd96..18ca10b 100644 --- a/fly.toml +++ b/fly.toml @@ -27,4 +27,4 @@ primary_region = "sea" [vm] size = "shared-cpu-1x" - memory = "1G" + memory = "1gb" From 8a56431b8fafa61ff0fa8a7bfb4822bbcdaa32d2 Mon Sep 17 00:00:00 2001 From: Mariano Uvalle Date: Wed, 24 Jan 2024 15:28:08 -0800 Subject: [PATCH 3/9] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 9ea2886..3e2d6a8 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 require ( github.com/gorilla/mux v1.8.0 github.com/mattn/go-isatty v0.0.20 - github.com/mattn/go-sqlite3 v1.14.18 + github.com/mattn/go-sqlite3 v1.14.19 github.com/oklog/ulid/v2 v2.1.0 github.com/spf13/cobra v1.8.0 golang.org/x/crypto v0.14.0 From d79d1419868af8ca5bd600ae94bcd4f2614291e3 Mon Sep 17 00:00:00 2001 From: Mariano Uvalle Date: Wed, 24 Jan 2024 23:31:20 +0000 Subject: [PATCH 4/9] Update go sum --- go.sum | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.sum b/go.sum index 7eeb01f..83bfc8b 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI= -github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI= +github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU= github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ= github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o= From 39f7e63c72f27b81c6ea10dff136f62c1b4719cc Mon Sep 17 00:00:00 2001 From: jmug Date: Thu, 19 Dec 2024 13:44:37 -0800 Subject: [PATCH 5/9] Add flake dev shell Signed-off-by: jmug --- .envrc | 1 + flake.nix | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 .envrc create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..03ff0d3 --- /dev/null +++ b/flake.nix @@ -0,0 +1,29 @@ +{ + description = "A basic flake with a shell"; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/release-24.11"; + systems.url = "github:nix-systems/default"; + flake-utils = { + url = "github:numtide/flake-utils"; + inputs.systems.follows = "systems"; + }; + }; + + outputs = + { nixpkgs, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + devShells.default = pkgs.mkShell { + packages = with pkgs; [ + go + gotools + gopls + ]; + }; + } + ); +} From a8a11ed86e410a13c5d4fa4db5ddf863efb80859 Mon Sep 17 00:00:00 2001 From: jmug Date: Thu, 19 Dec 2024 13:47:52 -0800 Subject: [PATCH 6/9] Ignore direnv cache Signed-off-by: jmug --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index c0136b7..758ad6d 100644 --- a/.gitignore +++ b/.gitignore @@ -93,3 +93,5 @@ tags [._]*.un~ # End of https://www.toptal.com/developers/gitignore/api/vim,linux,macos,go + +.direnv From 7ddf1162c7eb8b0914716ab6743fd002c57c8b02 Mon Sep 17 00:00:00 2001 From: jmug Date: Thu, 19 Dec 2024 13:47:57 -0800 Subject: [PATCH 7/9] flake lock Signed-off-by: jmug --- flake.lock | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 flake.lock diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..3a487c5 --- /dev/null +++ b/flake.lock @@ -0,0 +1,64 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": [ + "systems" + ] + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1734629298, + "narHash": "sha256-KOzg4I+sRJW6rFaBzVAXKKCi2/uhDgC8YWmImKG6cs8=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "fac643b357f5f203cc1fe07f3f070b7534d61334", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "release-24.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "systems": "systems" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} From c041a5feee83e3b97bcac559bb1dea42622e5ac6 Mon Sep 17 00:00:00 2001 From: Mariano Uvalle Date: Sat, 10 May 2025 13:59:51 -0700 Subject: [PATCH 8/9] Update to go 1.24 and add some logging. Signed-off-by: Mariano Uvalle --- .dockerignore | 99 ++++++++++++++++++++++++++++++++ cmd/admin/main.go | 16 ++++++ cmd/cli/main.go | 2 +- cmd/server/Dockerfile | 11 +--- flake.lock | 17 ++++++ flake.nix | 7 ++- fly.toml | 30 +++++----- go.mod | 4 +- internal/configfile/config.go | 4 +- internal/server/client/client.go | 5 ++ internal/server/client/key.go | 2 +- internal/server/logging.go | 9 +++ internal/server/server.go | 11 ++++ 13 files changed, 187 insertions(+), 30 deletions(-) create mode 100644 .dockerignore create mode 100644 cmd/admin/main.go create mode 100644 internal/server/logging.go diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b893687 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,99 @@ +# flyctl launch added from .gitignore +# Created by https://www.toptal.com/developers/gitignore/api/vim,linux,macos,go +# Edit at https://www.toptal.com/developers/gitignore?templates=vim,linux,macos,go + +### Go ### +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +**/*.exe +**/*.exe~ +**/*.dll +**/*.so +**/*.dylib + +# Test binary, built with `go test -c` +**/*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +**/*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +**/go.work + +### Linux ### +**/*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +**/.fuse_hidden* + +# KDE directory preferences +**/.directory + +# Linux trash folder which might appear on any partition or disk +**/.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +**/.nfs* + +### macOS ### +# General +**/.DS_Store +**/.AppleDouble +**/.LSOverride + +# Icon must end with two \r +**/Icon + + +# Thumbnails +**/._* + +# Files that might appear in the root of a volume +**/.DocumentRevisions-V100 +**/.fseventsd +**/.Spotlight-V100 +**/.TemporaryItems +**/.Trashes +**/.VolumeIcon.icns +**/.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +**/.AppleDB +**/.AppleDesktop +**/Network Trash Folder +**/Temporary Items +**/.apdisk + +### macOS Patch ### +# iCloud generated files +**/*.icloud + +### Vim ### +# Swap +**/[._]*.s[a-v][a-z] +!**/*.svg # comment out if you don't need vector files +**/[._]*.sw[a-p] +**/[._]s[a-rt-v][a-z] +**/[._]ss[a-gi-z] +**/[._]sw[a-p] + +# Session +**/Session.vim +**/Sessionx.vim + +# Temporary +**/.netrwhist +# Auto-generated tag files +**/tags +# Persistent undo +**/[._]*.un~ + +# End of https://www.toptal.com/developers/gitignore/api/vim,linux,macos,go + +**/.direnv +fly.toml diff --git a/cmd/admin/main.go b/cmd/admin/main.go new file mode 100644 index 0000000..8de1cff --- /dev/null +++ b/cmd/admin/main.go @@ -0,0 +1,16 @@ +package main + +import ( + "encoding/base64" + "fmt" + + "github.com/AYM1607/ccclip/pkg/crypto" +) + +func main() { + privK := crypto.NewPrivateKey() + privStr := base64.StdEncoding.EncodeToString(privK.Bytes()) + pubStr := base64.StdEncoding.EncodeToString(privK.PublicKey().Bytes()) + + fmt.Printf("Priv: %s\nPub: %s\n", privStr, pubStr) +} diff --git a/cmd/cli/main.go b/cmd/cli/main.go index f6facc6..f2e306e 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -7,7 +7,7 @@ import ( var apiclient *client.Client func init() { - apiclient = client.New("https://api.ccclip.io") + apiclient = client.New("https://clipboard.jmug.me") } func main() { diff --git a/cmd/server/Dockerfile b/cmd/server/Dockerfile index 41104b2..1825952 100644 --- a/cmd/server/Dockerfile +++ b/cmd/server/Dockerfile @@ -1,12 +1,7 @@ -FROM golang:1.21-alpine AS builder +FROM golang:1.24-alpine AS builder # Ensure we have a c compiler. RUN apk add --no-cache build-base ca-certificates fuse3 sqlite -# Install LiteFS for distribute SQLite -COPY --from=flyio/litefs:0.5 /usr/local/bin/litefs /usr/local/bin/litefs - -COPY cmd/server/litefs.yml /etc/litefs.yml - WORKDIR /src COPY go.mod . COPY go.sum . @@ -14,6 +9,6 @@ RUN go mod download COPY . . RUN go build -ldflags='-s -w' -tags 'linux' -trimpath -o /dist/app ./cmd/server -EXPOSE 3000 +EXPOSE 8080 -ENTRYPOINT ["litefs", "mount"] +ENTRYPOINT ["/dist/app"] diff --git a/flake.lock b/flake.lock index 3a487c5..b045e51 100644 --- a/flake.lock +++ b/flake.lock @@ -20,6 +20,22 @@ "type": "github" } }, + "gonixpkgs": { + "locked": { + "lastModified": 1746518791, + "narHash": "sha256-MiJ11L7w18S2G5ftcoYtcrrS0JFqBaj9d5rwJFpC5Wk=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1cb1c02a6b1b7cf67e3d7731cbbf327a53da9679", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1cb1c02a6b1b7cf67e3d7731cbbf327a53da9679", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1734629298, @@ -39,6 +55,7 @@ "root": { "inputs": { "flake-utils": "flake-utils", + "gonixpkgs": "gonixpkgs", "nixpkgs": "nixpkgs", "systems": "systems" } diff --git a/flake.nix b/flake.nix index 03ff0d3..54a4ddf 100644 --- a/flake.nix +++ b/flake.nix @@ -2,6 +2,7 @@ description = "A basic flake with a shell"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/release-24.11"; + gonixpkgs.url = "github:NixOS/nixpkgs/1cb1c02a6b1b7cf67e3d7731cbbf327a53da9679"; systems.url = "github:nix-systems/default"; flake-utils = { url = "github:numtide/flake-utils"; @@ -10,15 +11,17 @@ }; outputs = - { nixpkgs, flake-utils, ... }: + { nixpkgs, gonixpkgs, flake-utils, ... }: flake-utils.lib.eachDefaultSystem ( system: let pkgs = nixpkgs.legacyPackages.${system}; + gopkgs = gonixpkgs.legacyPackages.${system}; in { devShells.default = pkgs.mkShell { - packages = with pkgs; [ + packages = with gopkgs; [ + pkgs.flyctl go gotools gopls diff --git a/fly.toml b/fly.toml index 18ca10b..700ab2b 100644 --- a/fly.toml +++ b/fly.toml @@ -1,30 +1,30 @@ -# fly.toml app configuration file generated for dark-paper-8180 on 2023-11-10T08:23:22Z +# fly.toml app configuration file generated for ccclip on 2025-05-10T00:45:48-07:00 # # See https://fly.io/docs/reference/configuration/ for information about how to use this file. # -app = "dark-paper-8180" -primary_region = "sea" +app = 'ccclip' +primary_region = 'sea' [build] - dockerfile = "./cmd/server/Dockerfile" + dockerfile = './cmd/server/Dockerfile' [env] - CCCLIP_PORT = "3000" - CCCLIP_DATABASE_LOCATION = "/litefs/ccclip.db" + CCCLIP_DATABASE_LOCATION = '/database/ccclip.db' -[mounts] - source = "litefs" - destination = "/var/lib/litefs" +[[mounts]] + source = 'database' + destination = '/database' [http_service] internal_port = 8080 force_https = true - auto_stop_machines = true + auto_stop_machines = 'stop' auto_start_machines = true - min_machines_running = 1 - processes = ["app"] + processes = ['app'] -[vm] - size = "shared-cpu-1x" - memory = "1gb" +[[vm]] + size = 'shared-cpu-1x' + memory = '512mb' + cpu_kind = 'shared' + cpus = 1 diff --git a/go.mod b/go.mod index 3e2d6a8..4e167a3 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/AYM1607/ccclip -go 1.21 +go 1.24 + +toolchain go1.24.3 require ( github.com/gorilla/mux v1.8.0 diff --git a/internal/configfile/config.go b/internal/configfile/config.go index 6069612..4e927c5 100644 --- a/internal/configfile/config.go +++ b/internal/configfile/config.go @@ -55,12 +55,12 @@ func Write(c ConfigFile) error { func LoadPrivateKey() (*ecdh.PrivateKey, error) { fp := path.Join(Path, PrivateKeyFileName) - return crypto.LoadPrivateKeyFromFile(fp) + return crypto.LoadPrivateKeyFromFile(fp), nil } func LoadPublicKey() (*ecdh.PublicKey, error) { fp := path.Join(Path, PublicKeyFileName) - return crypto.LoadPublicKeyFromFile(fp) + return crypto.LoadPublicKeyFromFile(fp), nil } func SavePrivateKey(k *ecdh.PrivateKey) error { diff --git a/internal/server/client/client.go b/internal/server/client/client.go index a53761e..f558d52 100644 --- a/internal/server/client/client.go +++ b/internal/server/client/client.go @@ -5,6 +5,7 @@ import ( "crypto/ecdh" "encoding/json" "errors" + "fmt" "io" "net/http" "time" @@ -110,6 +111,8 @@ func (c *Client) SetClipboard(plaintext []byte, deviceId string, pvk *ecdh.Priva } hres, err := http.Post(c.url+"/setClipboard", "application/json", bytes.NewReader(authReqJson)) + hresBytes, _ := io.ReadAll(hres.Body) + fmt.Println(string(hresBytes)) if err != nil { return err } @@ -194,6 +197,8 @@ func (c *Client) getDevices(deviceId string, pvk *ecdh.PrivateKey) ([]*api.Devic hresBody, err := io.ReadAll(hres.Body) defer hres.Body.Close() + hresBytes, _ := io.ReadAll(hres.Body) + fmt.Println(string(hresBytes)) if err != nil { return nil, err } diff --git a/internal/server/client/key.go b/internal/server/client/key.go index db8ceda..da7b983 100644 --- a/internal/server/client/key.go +++ b/internal/server/client/key.go @@ -8,7 +8,7 @@ import ( "github.com/AYM1607/ccclip/pkg/crypto" ) -const serverPublicKeyB64 = "JTyaIVDHe1Nwqmd4NFlkvqj+MZOVp5s3JZP+T3QuoT8=" +const serverPublicKeyB64 = "Dg6HYJ8aoQOOzGqOCw4J7tnT+QHkokjfdeWM8ktwnks=" var serverPublicKey *ecdh.PublicKey diff --git a/internal/server/logging.go b/internal/server/logging.go new file mode 100644 index 0000000..ccb8697 --- /dev/null +++ b/internal/server/logging.go @@ -0,0 +1,9 @@ +package server + +import "log" + +var logger *log.Logger + +func init() { + logger = log.Default() +} diff --git a/internal/server/server.go b/internal/server/server.go index aaa74d9..e9274e7 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -166,9 +166,11 @@ type GetUserDevicesResponse struct { } func (c *controller) handleGetUserDevices(w http.ResponseWriter, r *http.Request) { + logger.Println("handling get user devices") var authReq AuthenticatedPayload err := json.NewDecoder(r.Body).Decode(&authReq) if err != nil { + logger.Printf("decoding request: %s\n", err) http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -177,18 +179,21 @@ func (c *controller) handleGetUserDevices(w http.ResponseWriter, r *http.Request // TODO: verify the request fingerprint. Right now we're just trusting that // if it decrypts successfully then we can trust it. if err != nil { + logger.Printf("decrypting payload: %s\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } user, err := c.store.GetDeviceUser(authReq.DeviceID) if err != nil { + logger.Printf("getting user for device: %s\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } devices, err := c.store.GetUserDevices(user.ID) if err != nil { + logger.Printf("getting devices for user: %s\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -197,6 +202,7 @@ func (c *controller) handleGetUserDevices(w http.ResponseWriter, r *http.Request w.WriteHeader(http.StatusOK) err = json.NewEncoder(w).Encode(res) if err != nil { + logger.Printf("encoding response: %s\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -208,9 +214,11 @@ type SetClipboardRequest struct { } func (c *controller) handleSetClipboard(w http.ResponseWriter, r *http.Request) { + log.Default().Println("handling set clipboard") var authReq AuthenticatedPayload err := json.NewDecoder(r.Body).Decode(&authReq) if err != nil { + log.Default().Printf("decoding request: %s\n", err) http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -219,18 +227,21 @@ func (c *controller) handleSetClipboard(w http.ResponseWriter, r *http.Request) // TODO: verify the request fingerprint. Right now we're just trusting that // if it decrypts successfully then we can trust it. if err != nil { + log.Default().Printf("decrypting authenticated payload: %s\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } user, err := c.store.GetDeviceUser(authReq.DeviceID) if err != nil { + log.Default().Printf("getting user device: %s\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } err = c.store.PutClipboard(user.ID, req.Clipboard) if err != nil { + log.Default().Printf("putting keyboard: %s\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } From 8445a7546ee81c7c6503401127bbc7b25cb14fee Mon Sep 17 00:00:00 2001 From: jmug Date: Sat, 10 May 2025 14:26:20 -0700 Subject: [PATCH 9/9] Allow registering device without requiring the creation of the config file. Signed-off-by: jmug --- cmd/cli/registerDevice.go | 13 ++++++++++--- internal/configfile/config.go | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/cli/registerDevice.go b/cmd/cli/registerDevice.go index 4d7a911..eb52387 100644 --- a/cmd/cli/registerDevice.go +++ b/cmd/cli/registerDevice.go @@ -12,6 +12,7 @@ import ( func init() { rootCmd.AddCommand(registerDeviceCommand) + registerDeviceCommand.Flags().StringVarP(&email, "email", "e", "", "email is your login identifier, ignored if your config file has has an email") } var registerDeviceCommand = &cobra.Command{ @@ -23,8 +24,11 @@ var registerDeviceCommand = &cobra.Command{ return err } - if cc.Email == "" { - return errors.New("you don't have an account configured for thist device") + if cc.Email != "" { + email = cc.Email + } + if email == "" { + return errors.New("provide an email through your config file or with --email") } if cc.DeviceId != "" { @@ -35,7 +39,7 @@ var registerDeviceCommand = &cobra.Command{ pbk := pvk.PublicKey() password := input.ReadPassword() - res, err := apiclient.RegisterDevice(cc.Email, password, pbk.Bytes()) + res, err := apiclient.RegisterDevice(email, password, pbk.Bytes()) if err != nil { return err } @@ -43,6 +47,9 @@ var registerDeviceCommand = &cobra.Command{ // Write the key files first, if those fail to write then we should not // save the device Id. cc.DeviceId = res.DeviceID + if cc.Email == "" { + cc.Email = email + } err = configfile.SavePrivateKey(pvk) if err != nil { return err diff --git a/internal/configfile/config.go b/internal/configfile/config.go index 4e927c5..53c3796 100644 --- a/internal/configfile/config.go +++ b/internal/configfile/config.go @@ -22,7 +22,7 @@ type ConfigFile struct { var Path string func EnsureAndGet() (ConfigFile, error) { - err := os.MkdirAll(Path, os.FileMode(int(0660))) + err := os.MkdirAll(Path, os.FileMode(int(0770))) if err != nil { return ConfigFile{}, fmt.Errorf("could not create config directory: %w", err) } @@ -38,7 +38,7 @@ func EnsureAndGet() (ConfigFile, error) { } func Write(c ConfigFile) error { - err := os.MkdirAll(Path, os.FileMode(int(0660))) + err := os.MkdirAll(Path, os.FileMode(int(0770))) if err != nil { return err }