From 0b9bdba0225e86002e4dbcc78f62e69b80e062c0 Mon Sep 17 00:00:00 2001 From: Haruki Date: Fri, 25 Aug 2023 09:46:44 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=9F=E8=A1=8C=E4=B8=AD=E3=81=AE?= =?UTF-8?q?=E3=83=90=E3=82=A4=E3=83=8A=E3=83=AA=E3=81=AE=E7=B5=B6=E5=AF=BE?= =?UTF-8?q?=E3=83=91=E3=82=B9=E3=82=92=E3=82=82=E3=81=A8=E3=81=ABcute-ostr?= =?UTF-8?q?ich.txt=E3=82=92=E5=8F=96=E5=BE=97=E3=81=99=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nostrh/cmd/paths/paths.go | 12 ++++++++++++ nostrh/main.go | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/nostrh/cmd/paths/paths.go b/nostrh/cmd/paths/paths.go index de52894..4c400ef 100644 --- a/nostrh/cmd/paths/paths.go +++ b/nostrh/cmd/paths/paths.go @@ -26,3 +26,15 @@ func GetSettingsDirectory() (string, error) { return dirPath, nil } + +func GetProjectRootDirectory() (string, error) { + // 実行中のバイナリの絶対パスを取得 + exePath, err := os.Executable() + if err != nil { + return "", err + } + + // ディレクトリパスを取得 + dir := filepath.Dir(exePath) + return dir, nil +} diff --git a/nostrh/main.go b/nostrh/main.go index af535fc..e7f9a70 100644 --- a/nostrh/main.go +++ b/nostrh/main.go @@ -4,15 +4,19 @@ import ( "bytes" "fmt" "os" + "path/filepath" "github.com/nbd-wtf/go-nostr" "github.com/studiokaiji/nostr-webhost/nostrh/cmd/deploy" "github.com/studiokaiji/nostr-webhost/nostrh/cmd/keystore" + "github.com/studiokaiji/nostr-webhost/nostrh/cmd/paths" "github.com/studiokaiji/nostr-webhost/nostrh/cmd/relays" "github.com/studiokaiji/nostr-webhost/nostrh/cmd/server" "github.com/urfave/cli/v2" ) +const cuteOstrichFileName = "cute-ostrich.txt" + func main() { var ( port string @@ -136,7 +140,13 @@ func main() { if len(os.Args) < 2 || os.Args[1] == "help" || os.Args[1] == "h" { // Display ostrich - file, err := os.Open("./cute-ostrich.txt") + projectDir, err := paths.GetProjectRootDirectory() + if err != nil { + fmt.Println(err) + } + + filePath := filepath.Join(projectDir, cuteOstrichFileName) + file, err := os.Open(filePath) if err != nil { fmt.Println(err) } @@ -149,7 +159,7 @@ func main() { break } if err != nil { - panic(err) + break } fmt.Println(buf.String()) From ca27d3f1f339d7aecd9163172c800e1bf6f3d7ba Mon Sep 17 00:00:00 2001 From: Haruki Date: Fri, 25 Aug 2023 09:55:16 +0900 Subject: [PATCH 2/2] =?UTF-8?q?go=20embed=E3=81=A7txt=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=A4=E3=83=AB=E3=82=92=E5=9F=8B=E3=82=81=E8=BE=BC=E3=82=93?= =?UTF-8?q?=E3=81=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nostrh/main.go | 32 ++++---------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/nostrh/main.go b/nostrh/main.go index e7f9a70..2bc4f89 100644 --- a/nostrh/main.go +++ b/nostrh/main.go @@ -1,21 +1,20 @@ package main import ( - "bytes" + _ "embed" "fmt" "os" - "path/filepath" "github.com/nbd-wtf/go-nostr" "github.com/studiokaiji/nostr-webhost/nostrh/cmd/deploy" "github.com/studiokaiji/nostr-webhost/nostrh/cmd/keystore" - "github.com/studiokaiji/nostr-webhost/nostrh/cmd/paths" "github.com/studiokaiji/nostr-webhost/nostrh/cmd/relays" "github.com/studiokaiji/nostr-webhost/nostrh/cmd/server" "github.com/urfave/cli/v2" ) -const cuteOstrichFileName = "cute-ostrich.txt" +//go:embed cute-ostrich.txt +var cuteOstrich string func main() { var ( @@ -140,30 +139,7 @@ func main() { if len(os.Args) < 2 || os.Args[1] == "help" || os.Args[1] == "h" { // Display ostrich - projectDir, err := paths.GetProjectRootDirectory() - if err != nil { - fmt.Println(err) - } - - filePath := filepath.Join(projectDir, cuteOstrichFileName) - file, err := os.Open(filePath) - if err != nil { - fmt.Println(err) - } - defer file.Close() - - buf := new(bytes.Buffer) - for { - n, err := buf.ReadFrom(file) - if n == 0 { - break - } - if err != nil { - break - } - - fmt.Println(buf.String()) - } + fmt.Println(cuteOstrich) } // Start app