Skip to content

Commit

Permalink
add embed lib to create and skaffold aoc projects
Browse files Browse the repository at this point in the history
Signed-off-by: Rodolfo Sanchez <[email protected]>
  • Loading branch information
dolfolife committed Feb 21, 2024
1 parent 4d841fd commit ada1d05
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 61 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/dolfolife/aoctl#section-readme.svg)](https://pkg.go.dev/github.com/dolfolife/aoctl#section-readme)
[![codecov](https://codecov.io/github/dolfolife/aoctl/graph/badge.svg?token=GTFZX1J2WX)](https://codecov.io/github/dolfolife/aoctl)

This is a personal project I have to learn more about writing CLI tools in GoLang. The idea behind this project is to help me solve the [Advent of Code](adventofcode.com/) since I have noticed some patterns while solving them.
This is a personal project for myself to learn more about writing CLI tools in GoLang. The idea behind this project is to help me solve the [Advent of Code](adventofcode.com/) since I have noticed some patterns while solving them.

> Disclaimer: This is yet another CLI that solves a common problem and there are probably N solutions out there. My motivation is not only to provide a CLI but to learn how to write, develop, release, and test them.
> Disclaimer: This is yet another CLI that solves a common problem and there are probably `n` solutions out there. My motivation is not only to provide a CLI but to learn how to write, develop, release, and test them.
I hope you enjoy it and feel free to help me understand the best practices of writing CLI tools or GoLang packages by writing an issue or using the [CONTRIBUTING.md](./CONTRIBUTING.md) to write a PR.
I hope you enjoy it and feel free to help me understand the best practices of writing CLI tools or GoLang packages by writing an issue or using the [CONTRIBUTING.md](./CONTRIBUTING.md) to contribute. PRs are welcome.

## Advent Of Code Series

Advent of Code is a website made by Eric Wastl. I recommend watching [Advent of Code: Behind the Scenes](https://www.youtube.com/watch?v=CFWuwNDOnIo&ab_channel=CodingTech).

## Quick Note
## The Why

The inspiration to build a tool around solving comes from laziness of not going to a browser to find puzzles and submit my answers.
Advent of Code application only cares about your solution, but there is no way to link your code and the problem at hand. The main idea of this tool is to give you a link between the application of [adventofcode.com](https://adventofcode.com/) and your working space.

I found [Advent Of Code Go](https://github.com/alexchao26/advent-of-code-go) and it inspired me to build my own tool and learn more GoLang.
## Alternatives

I found [Advent Of Code Go](https://github.com/alexchao26/advent-of-code-go) and it inspired me to build my own tool and learn more GoLang.

# AoC CLI Documentation

Expand Down
9 changes: 4 additions & 5 deletions adrs/ux_get_puzzle_information.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ What questions should we ask?
## Get
The user will write `aoctl puzzle get/sync` with some informaation


Information needed:
- Year // if year is not provided it will go over 2015-lastDecemberyear
- Day // if day is not provided it will go over 1-25. If we are between dec 1 and dec 25 and YEAR is current, only do until "TODAY's day"
Expand All @@ -31,7 +30,6 @@ The user should expect a file structure for each year/day
- Test files for each part with the description of the part as comments in the file.
- Readme file that explains how to use each file of this puzzle solver


puzzle.yaml
- language this overwrites the root language runtime
- Language version
Expand All @@ -47,10 +45,11 @@ Example of a aocproject files:

/<custom-name-for-project>
README.md
.gitignore // the session cookie file should be here
.env// the session cookie file should be here
.gitignore //
.git/ // initialized git with provided remote or empty
.aoc.yaml // configuration like language, and other preferences
/src
.aocconfig.yaml // configuration like language, and other preferences
/events
/<year>
/<day>
README.md
Expand Down
37 changes: 18 additions & 19 deletions cmd/aoctl/init.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2023 Rodolfo Sanchez <[email protected]>
Copyright © 2024 Rodolfo Sanchez <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,29 +22,28 @@ THE SOFTWARE.
package aoctl

import (

"github.com/spf13/cobra"
"github.com/dolfolife/aoctl/pkg/aoc"
"github.com/dolfolife/aoctl/pkg/aoc"
"github.com/spf13/cobra"
)

var initCmd = &cobra.Command{
Use: "init",
Aliases: []string{"initialize"},
Short: "Initialize the Advent of Code project in the path specifed",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
path := cmd.Flags().Lookup("path").Value.String()

if path == "" {
path= "adventofcode"
}

aoc.InitializeProject(path)
},
Use: "init",
Aliases: []string{"initialize"},
Short: "Initialize the Advent of Code project in the path specifed",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
path := cmd.Flags().Lookup("path").Value.String()

if path == "" {
path = "adventofcode"
}

aoc.InitializeProject(path)
},
}

func init() {
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(initCmd)

initCmd.Flags().StringP("path", "p", "", "Path to initialize the project")
initCmd.Flags().StringP("path", "p", "", "Path to initialize the project")
}
19 changes: 19 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Reference


### Project Structure

The key part of the project is to organize all the advent of code events by year. Each year will const of a matrix of days and problems (2 problems per day).

```bash
.env # environment variables file
adventofcode.yaml # configuration file for the project
README.md #
events/ # root of all the years of advent of code you have worked on
<year>/
<day>/
<solution-files> # Test and code of your solutions
inputs/ # inputs for testing and submiting the anwser online
metadata.yaml # information of the current state of your solution
README.md # Description of the problems (cache from adventofcode.com/year/day)
```
50 changes: 19 additions & 31 deletions pkg/aoc/aoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,41 @@ func InitializeProject(path string) error {
err := os.Mkdir(fullPath, os.ModePerm)

if err != nil {
log.Fatalf("Error creating directory: %s", err)
log.Fatalf("Error creating directory at %s: %s", fullPath, err)
return err
}

cookieFile := filepath.Join(path, ".env")
roFile, err := os.OpenFile(cookieFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Error creating cookie file: %s", err)
if err := createFileFromTemplates(filepath.Join(path, ".env"), GetRootFile("env")); err != nil {
log.Fatalf("Error writing the env file: %s", err)
return err
}

defer roFile.Close()

_, err = roFile.WriteString(`
# Use the .aoc file to set the environment variables for your project
AOC_SESSION=<insert-advent-of-code-session>
`)

if err != nil {
log.Fatalf("Error writing to the aoc file: %s", err)
if err = createFileFromTemplates(filepath.Join(path, "README.md"), GetRootFile("README.md")); err != nil {
log.Fatalf("Error creating README.md file: %s", err)
return err
}

readmeFile := filepath.Join(path, "README.md")
roFile, err = os.OpenFile(readmeFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Error creating README.md file: %s", err)
if err = createFileFromTemplates(filepath.Join(path, ".gitignore"), GetRootFile("gitignore")); err != nil {
log.Fatalf("Error creating gitignore file: %s", err)
return err
}

defer roFile.Close()
roFile.WriteString(`
# Advent of Code
`)
log.Println("Project initialized!")
return nil
}

gitIgnoreFile := filepath.Join(path, ".gitignore")
roFile, err = os.OpenFile(gitIgnoreFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
func createFileFromTemplates(file string, content string) error {
roFile, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Error creating .gitignore file: %s", err)
return err
}

defer roFile.Close()

if _, err = roFile.WriteString(`
# Advent of Code use the .aoc file for storing session information which you should
# not track in source control.
.env
`); err != nil {
if _, err = roFile.WriteString(content); err != nil {
return err
}

log.Println("Project initialized")
return nil
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/aoc/template_files_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package aoc

import (
"embed"
"fmt"
"log"
)

//go:embed templates
var templates embed.FS

func GetRootFile(filename string) string {
data, err := templates.ReadFile(fmt.Sprintf("templates/root/%s", filename))

if err != nil {
log.Fatalf("error reading %s template with: \n%v\n", filename, err)
}

return string(data)
}
1 change: 1 addition & 0 deletions pkg/aoc/templates/root/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Advent of Code Solutions
2 changes: 2 additions & 0 deletions pkg/aoc/templates/root/env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Use the .env file to set the environment variables for your project
AOC_SESSION=<insert-advent-of-code-session>
1 change: 1 addition & 0 deletions pkg/aoc/templates/root/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env

0 comments on commit ada1d05

Please sign in to comment.