From cd612f03ad6577e1fb582b6d76ca0813d4453d9d Mon Sep 17 00:00:00 2001 From: Artur Manuel Date: Sun, 11 Aug 2024 22:44:19 +0100 Subject: [PATCH] feat(go): add go as a template add go as a template when i need to --- flake.lock | 27 +++++++++ flake.nix | 10 +++- templates/go/flake.lock | 27 +++++++++ templates/go/flake.nix | 29 ++++++++++ templates/go/go.mod | 3 + templates/go/main.go | 108 +++++++++++++++++++++++++++++++++++ templates/go/nix/package.nix | 10 ++++ templates/go/nix/shell.nix | 11 ++++ 8 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 flake.lock create mode 100644 templates/go/flake.lock create mode 100644 templates/go/flake.nix create mode 100644 templates/go/go.mod create mode 100644 templates/go/main.go create mode 100644 templates/go/nix/package.nix create mode 100644 templates/go/nix/shell.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..604641c --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1723400035, + "narHash": "sha256-WoKZDlBEdMhP+hjquBAh0BhUJbcH2+U8g2mHOr1mv8I=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "a731b45590a5169542990c36ffcde6cebd9a3356", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "release-24.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix index 7d68532..e57e9d2 100644 --- a/flake.nix +++ b/flake.nix @@ -1,12 +1,20 @@ { description = "Flake Template Hell"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/release-24.05"; + }; - outputs = _: { + outputs = _: { templates = { "python" = { path = ./templates/python; description = "python flake template"; }; + "go" = { + path = ./templates/go; + description = "go flake template"; + }; }; }; } diff --git a/templates/go/flake.lock b/templates/go/flake.lock new file mode 100644 index 0000000..604641c --- /dev/null +++ b/templates/go/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1723400035, + "narHash": "sha256-WoKZDlBEdMhP+hjquBAh0BhUJbcH2+U8g2mHOr1mv8I=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "a731b45590a5169542990c36ffcde6cebd9a3356", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "release-24.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/templates/go/flake.nix b/templates/go/flake.nix new file mode 100644 index 0000000..e6da422 --- /dev/null +++ b/templates/go/flake.nix @@ -0,0 +1,29 @@ +{ + description = "Go flake template"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/release-24.05"; + }; + + outputs = { self, nixpkgs }: + let + allSystems = [ + "x86_64-linux" + "aarch64-linux" + ]; + forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { + pkgs = import nixpkgs { inherit system; }; + inherit system; + }); + in + { + packages = forAllSystems ({ pkgs, system }: { + default = pkgs.callPackage ./nix/package.nix { }; + go-app = self.packages.${system}.default; + }); + + devShells = forAllSystems ({ pkgs, system }: { + default = pkgs.callPackage ./nix/shell.nix { inherit (self.packages.${system}) go-app; }; + }); + }; +} diff --git a/templates/go/go.mod b/templates/go/go.mod new file mode 100644 index 0000000..ecc0a6e --- /dev/null +++ b/templates/go/go.mod @@ -0,0 +1,3 @@ +module codeberg.org/amadaimbra/flake-template-hell + +go 1.22.5 diff --git a/templates/go/main.go b/templates/go/main.go new file mode 100644 index 0000000..3d1e314 --- /dev/null +++ b/templates/go/main.go @@ -0,0 +1,108 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "log" + "net/http" + "os" + "time" +) + +type Quote struct { + Quote string `json:"quote"` + Author string `json:"author"` +} + +func (q Quote) PrintQuote() { + fmt.Printf("\"%s\" - %s\n", q.Quote, q.Author) +} + +func GetResponse() (*http.Response, error) { + var url string = "https://dummyjson.com/quotes/random" + var res *http.Response + var err error + var attempts int = 4 + + for attempts != 0 { + res, err = http.Get(url) + if err != nil { + fmt.Fprintln(os.Stderr, "Error encountered... trying again...") + time.Sleep(2 * time.Second) + attempts-- + continue + } + + if res.StatusCode != 200 { + fmt.Fprintf(os.Stderr, "API returned but a 200, so I will be running this again in a few seconds.") + time.Sleep(2 * time.Second) + attempts-- + continue + } + + if res.StatusCode == 200 { + return res, nil + } + } + + if err != nil { + return nil, err + } + + if res.StatusCode != 200 { + return nil, errors.New("Gave up attempting to get 200.") + } + + return res, nil +} + +func GetBodyFromResponse(res *http.Response) ([]byte, error) { + var body []byte + var err error + + defer res.Body.Close() + body, err = io.ReadAll(res.Body) + if err != nil { + return nil, err + } + + return body, nil +} + +func NewQuoteFromBody(body []byte) (Quote, error) { + var quote Quote + var err error + + err = json.Unmarshal(body, "e) + if err != nil { + return Quote{}, err + } + + return quote, nil +} + +func main() { + var err error + var res *http.Response + + res, err = GetResponse() + if err != nil { + log.Fatalln(err) + } + + var body []byte + body, err = GetBodyFromResponse(res) + if err != nil { + log.Fatalln(err) + } + + var quote Quote + quote, err = NewQuoteFromBody(body) + if err != nil { + log.Fatalln(err) + } + + quote.PrintQuote() +} diff --git a/templates/go/nix/package.nix b/templates/go/nix/package.nix new file mode 100644 index 0000000..c3757ec --- /dev/null +++ b/templates/go/nix/package.nix @@ -0,0 +1,10 @@ +{ buildGoModule, lib }: +let + pname = "go-app"; + version = "0.1.0"; +in +buildGoModule { + inherit pname version; + src = ../.; + vendorHash = null; +} diff --git a/templates/go/nix/shell.nix b/templates/go/nix/shell.nix new file mode 100644 index 0000000..c0e0d93 --- /dev/null +++ b/templates/go/nix/shell.nix @@ -0,0 +1,11 @@ +{ mkShell, go, go-app, gopls, go-tools, gotools }: +mkShell { + name = "Go Development Shell"; + inputsFrom = [ go-app ]; + buildInputs = [ + go + gopls + go-tools + gotools + ]; +}