feat(go): add go as a template
add go as a template when i need to
This commit is contained in:
parent
edcf6f758e
commit
cd612f03ad
8 changed files with 224 additions and 1 deletions
27
flake.lock
generated
Normal file
27
flake.lock
generated
Normal file
|
@ -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
|
||||||
|
}
|
10
flake.nix
10
flake.nix
|
@ -1,12 +1,20 @@
|
||||||
{
|
{
|
||||||
description = "Flake Template Hell";
|
description = "Flake Template Hell";
|
||||||
|
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:nixos/nixpkgs/release-24.05";
|
||||||
|
};
|
||||||
|
|
||||||
outputs = _: {
|
outputs = _: {
|
||||||
templates = {
|
templates = {
|
||||||
"python" = {
|
"python" = {
|
||||||
path = ./templates/python;
|
path = ./templates/python;
|
||||||
description = "python flake template";
|
description = "python flake template";
|
||||||
};
|
};
|
||||||
|
"go" = {
|
||||||
|
path = ./templates/go;
|
||||||
|
description = "go flake template";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
27
templates/go/flake.lock
generated
Normal file
27
templates/go/flake.lock
generated
Normal file
|
@ -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
|
||||||
|
}
|
29
templates/go/flake.nix
Normal file
29
templates/go/flake.nix
Normal file
|
@ -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; };
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
3
templates/go/go.mod
Normal file
3
templates/go/go.mod
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
module codeberg.org/amadaimbra/flake-template-hell
|
||||||
|
|
||||||
|
go 1.22.5
|
108
templates/go/main.go
Normal file
108
templates/go/main.go
Normal file
|
@ -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()
|
||||||
|
}
|
10
templates/go/nix/package.nix
Normal file
10
templates/go/nix/package.nix
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{ buildGoModule, lib }:
|
||||||
|
let
|
||||||
|
pname = "go-app";
|
||||||
|
version = "0.1.0";
|
||||||
|
in
|
||||||
|
buildGoModule {
|
||||||
|
inherit pname version;
|
||||||
|
src = ../.;
|
||||||
|
vendorHash = null;
|
||||||
|
}
|
11
templates/go/nix/shell.nix
Normal file
11
templates/go/nix/shell.nix
Normal file
|
@ -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
|
||||||
|
];
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue