Skip to content

Commit

Permalink
fix: add a lot of comments for the purpose of maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
talwat committed Apr 21, 2023
1 parent 1f53b58 commit ee9f7d3
Show file tree
Hide file tree
Showing 23 changed files with 81 additions and 9 deletions.
3 changes: 2 additions & 1 deletion internal/cmd/downloadcmds/downloadcmds.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// These commands just get the url, download the jarfile, and checksum it.
// Because not everyone uses SHA256 for checksumming, it's safe to ignore gosec warnings.
// Because not everyone uses SHA256 for checksumming,
// it's safe to ignore gosec warnings about insecure hash algorithms.
package downloadcmds
3 changes: 3 additions & 0 deletions internal/cmd/downloadcmds/fabric.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package downloadcmds

// Fabric unfortunately does not have checksums available.
// If I am wrong about this, feel free to open an issue.

import (
"github.com/talwat/pap/internal/fs"
"github.com/talwat/pap/internal/global"
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/downloadcmds/paper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/urfave/cli/v2"
)

// This will probably have some kind of issue eventually with a valid version being mistaken as invalid.
// But it does help of quickly giving the user feedback that they made a typo in the version number.
func validatePaperOptions() {
const latest = "latest"

Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/eula.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package cmd

// The eula/sign command.
// The internal naming of this is a bit confusing, but eula and sign both refer to the same command.

import (
"fmt"

Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/geyser.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cmd

// This should be purged after the 1.0 release including the entry in the main.go file.

import (
"github.com/talwat/pap/internal/log"
"github.com/urfave/cli/v2"
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/plugincmds/generatecmds/generatecmds.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// pap plugin generate definitions.
// Pretty simple, most of them just call the Generate() function and that's it.
package generatecmds

import (
Expand All @@ -11,6 +13,7 @@ import (
"github.com/talwat/pap/internal/plugins/sources/paplug"
)

// Generates and writes the converted plugin.
func Generate(getPluginInfo func(plugin string) paplug.PluginInfo, plugins []string) {
if len(plugins) == 0 {
log.RawError("you must specify plugins to generate")
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/script.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package cmd

// The entire `script` command is defined here.
// It could if deemed useful be split up into several files,
// And put in it's own directory which is inside `internal`.

import (
"fmt"
"runtime"
Expand Down
4 changes: 4 additions & 0 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import (
"github.com/talwat/pap/internal/log"
)

// Check if a binary exists on the system, for example, `ls`.
func CommandExists(cmd string) bool {
_, err := exec.LookPath(cmd)

return !errors.Is(err, exec.ErrNotFound)
}

// Runs a command and uses a bunch of dots after the log to display progress.
// Whenever the command outputs something to stdout or stderr, it will output a '.'.
// So it would look something like: 'pap: running command go build...........'.
func Run(workDir string, cmd string) int {
log.NoNewline("running command %s", cmd)

Expand Down
3 changes: 2 additions & 1 deletion internal/fs/fs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Filesystem Management
// Filesystem Management.
// Basically just the fs parts of the os package but with debug logging and error handling.
package fs

import (
Expand Down
2 changes: 1 addition & 1 deletion internal/fs/unzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func commandUnzip(src string, dest string) int {

// Full golang implementation, refactoring and editing is needed.
// This function is avoided if possible, but kept just in case the user doesn't have basic utilities.
// I mean seriously, who doesn't have tar/unzip?
// I mean seriously, who doesn't have unzip?
//
//nolint:goerr113,funlen,wrapcheck // I have no idea how to shorten this mess.
func unsafeUnzip(src string, dest string) {
Expand Down
1 change: 1 addition & 0 deletions internal/jarfiles/fabric/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func GetURL(versionInput string, loaderInput string, installerInput string) stri
loader := loaderInput
installer := installerInput

// A bit of repetitive code, but I am not willing to use generics to do this.
if version == jarfiles.Latest {
versions := GetMinecraftVersions()
version = getLatestVersion(versions).Version
Expand Down
3 changes: 3 additions & 0 deletions internal/jarfiles/jarfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func UnsupportedMessage() {
log.Warn("additionally, plugins from the plugin manager will not work properly")
}

// Verifies a jarfile by using it's calculated checksum from the net.Download function.
// If compares the hex encoded checksum with `proper`.
func VerifyJarfile(calculated []byte, proper string) {
log.Log("verifying downloaded jarfile...")

Expand All @@ -30,6 +32,7 @@ func VerifyJarfile(calculated []byte, proper string) {
}
}

// An API error. If the `err` string isn't empty (undefined), then it will spit out an error.
func APIError(err string, statusCode int) {
if err != "" {
log.RawError("api returned an error with status code %d: %s", statusCode, FormatErrorMessage(err))
Expand Down
4 changes: 3 additions & 1 deletion internal/jarfiles/official/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"

"github.com/talwat/pap/internal/global"
"github.com/talwat/pap/internal/jarfiles"
"github.com/talwat/pap/internal/log"
"github.com/talwat/pap/internal/net"
)

// Finds the version in a list of versions.
func FindVersion(versions Versions, version string) Version {
for i := range versions.Versions {
if versions.Versions[i].ID == version {
Expand Down Expand Up @@ -74,7 +76,7 @@ func GetLatestPackage() Package {
}

func GetPackage(versionInput string) Package {
if versionInput == "latest" {
if versionInput == jarfiles.Latest {
return GetLatestPackage()
}

Expand Down
15 changes: 11 additions & 4 deletions internal/jarfiles/paper/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/talwat/pap/internal/net"
)

// Gets the latest minecraft version in the list of versions.
func GetLatestVersion() string {
var versions Versions

Expand All @@ -25,6 +26,7 @@ func GetLatestVersion() string {
return version
}

// Gets the latest build in a version.
func GetLatestBuild(version string) Build {
var builds Builds

Expand Down Expand Up @@ -56,6 +58,7 @@ func GetLatestBuild(version string) Build {
return latest
}

// Gets a specific build in a version.
func GetSpecificBuild(version string, buildID string) Build {
log.Log("getting build information for %s", buildID)

Expand All @@ -69,10 +72,11 @@ func GetSpecificBuild(version string, buildID string) Build {
return build
}

// Gets either the latest build or a specific one depending on buildID.
func GetBuild(version string, buildID string) Build {
var build Build

if buildID == "latest" {
if buildID == jarfiles.Latest {
build = GetLatestBuild(version)
} else {
build = GetSpecificBuild(version, buildID)
Expand All @@ -88,10 +92,13 @@ func GetBuild(version string, buildID string) Build {
return build
}

func GetVersion(versionInput string) string {
if versionInput == jarfiles.Latest {
// Gets a specific version or the latest one depending on the input.
// Additionally, if using a specific one, it will not get the latest release of a specific minor release.
// For example if you put in 1.12, you will get 1.12 and not 1.12.2.
func GetVersion(version string) string {
if version == jarfiles.Latest {
return GetLatestVersion()
}

return versionInput
return version
}
6 changes: 6 additions & 0 deletions internal/jarfiles/purpur/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/talwat/pap/internal/net"
)

// Gets the latest version's info.
func GetLatestVersion() Version {
log.Log("getting versions...")

Expand Down Expand Up @@ -41,6 +42,7 @@ func GetLatestVersion() Version {
return version
}

// Gets a specific version's info.
func GetSpecificVersion(versionID string) Version {
log.Log("getting info for %s...", versionID)

Expand All @@ -60,6 +62,7 @@ func GetSpecificVersion(versionID string) Version {
return version
}

// Gets the latest build using the provided version information.
func GetLatestBuild(version Version) Build {
log.Log("getting latest build info...")

Expand All @@ -81,6 +84,7 @@ func GetLatestBuild(version Version) Build {
return build
}

// Gets a specific build using the provided version.
func GetSpecificBuild(version Version, buildID string) Build {
log.Log("getting build info for %s...", buildID)

Expand All @@ -100,6 +104,7 @@ func GetSpecificBuild(version Version, buildID string) Build {
return build
}

// Gets a build. It will be the latest one depending on what the input is.
func GetBuild(version Version, buildInput string) Build {
if buildInput == jarfiles.Latest {
return GetLatestBuild(version)
Expand All @@ -108,6 +113,7 @@ func GetBuild(version Version, buildInput string) Build {
return GetSpecificBuild(version, buildInput)
}

// Gets a version. It will be the latest one depending on what the input is.
func GetVersion(versionInput string) Version {
if versionInput == jarfiles.Latest {
return GetLatestVersion()
Expand Down
1 change: 1 addition & 0 deletions internal/log/color/color.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Just a bunch of constants for colors.
package color

const (
Expand Down
2 changes: 2 additions & 0 deletions internal/log/color/init_windows.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package color

// Windows is weird, but this is supposed to enable color on the old fashioned CMD.

import (
"os"

Expand Down
19 changes: 19 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"github.com/talwat/pap/internal/log/color"
)

// Checks if err != nil and if so exits.
func Error(err error, msg string, params ...interface{}) {
if err != nil {
RawError("%s: %s", fmt.Sprintf(msg, params...), err)
os.Exit(1)
}
}

// Like Error but spits out a newline before.
func NewlineBeforeError(err error, msg string, params ...interface{}) {
if err != nil {
RawLog("\n")
Expand All @@ -26,14 +28,18 @@ func NewlineBeforeError(err error, msg string, params ...interface{}) {
}
}

// Basic log message including the pap: prefix.
func Log(msg string, params ...interface{}) {
RawLog("pap: %s\n", fmt.Sprintf(msg, params...))
}

// Like Log but without a newline at the end.
func NoNewline(msg string, params ...interface{}) {
RawLog("pap: %s", fmt.Sprintf(msg, params...))
}

// Basically just fmt.Fprintf.
// Use this function in case pap decides to add a log file or whatever.
func RawLog(msg string, params ...interface{}) {
fmt.Fprintf(os.Stderr, msg, params...)
}
Expand All @@ -45,32 +51,41 @@ func OutputLog(msg string, params ...interface{}) {
fmt.Fprintf(os.Stdout, "%s\n", fmt.Sprintf(msg, params...))
}

// Prints out an error message and exits regardless.
func RawError(msg string, params ...interface{}) {
Log("%serror%s: %s", color.BrightRed, color.Reset, fmt.Sprintf(msg, params...))
os.Exit(1)
}

// Prints out a warning.
func Warn(msg string, params ...interface{}) {
Log("%swarning%s: %s", color.Yellow, color.Reset, fmt.Sprintf(msg, params...))
}

// Prints out a debug message.
// Debug info should be internal info that makes it easier to debug pap.
// Usually the information outputted here is completely useless to the end user.
func Debug(msg string, params ...interface{}) {
if global.Debug {
Log("%sdebug%s: %s", color.Magenta, color.Reset, fmt.Sprintf(msg, params...))
}
}

// Like Debug, but with a newline before.
func NewlineBeforeDebug(msg string, params ...interface{}) {
if global.Debug {
RawLog("\n")
Log("%sdebug%s: %s", color.Magenta, color.Reset, fmt.Sprintf(msg, params...))
}
}

// Prints out a success message.
// Whenever a "major" operation finishes, you can use this.
func Success(msg string, params ...interface{}) {
Log("%ssuccess%s: %s", color.Green, color.Reset, fmt.Sprintf(msg, params...))
}

// Scans standard input until it reaches a newline.
func RawScan() string {
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
Expand All @@ -79,6 +94,7 @@ func RawScan() string {
return strings.TrimSpace(text)
}

// Scan but it also handles the -y flag and has some nice looking logs.
func Scan(defaultVal string, prompt string, params ...interface{}) string {
NoNewline("%s (default %s): ", fmt.Sprintf(prompt, params...), defaultVal)

Expand All @@ -98,6 +114,8 @@ func Scan(defaultVal string, prompt string, params ...interface{}) string {
return input
}

// A yes or no prompt.
// Different from Continue because it won't exit if you say no.
func YesOrNo(defaultVal string, prompt string, params ...interface{}) bool {
NoNewline("%s [y/n]: ", fmt.Sprintf(prompt, params...))

Expand All @@ -117,6 +135,7 @@ func YesOrNo(defaultVal string, prompt string, params ...interface{}) bool {
return input == "y"
}

// A continue prompt. If the user puts out anything that isn't "y" (case insensitive), then it will exit.
func Continue(prompt string, params ...interface{}) {
NoNewline("%s [y/n]: ", fmt.Sprintf(prompt, params...))

Expand Down
4 changes: 3 additions & 1 deletion internal/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/talwat/pap/internal/log"
)

// Makes and executes an HTTP request with proper headers identifying pap.
func DoRequest(url string, notFoundMsg string) *http.Response {
log.Debug("making a new request to %s", url)

Expand Down Expand Up @@ -68,7 +69,8 @@ func Get(url string, notFoundMsg string, content interface{}) int {
return resp.StatusCode
}

// Set hash to nil in order to disable checksumming.
// Downloads a file. If the file is too small, a progress bar won't be displayed.
// Set hash to nil in order to prevent calculating the hash.
func Download(
url string,
notFoundMsg string,
Expand Down
1 change: 1 addition & 0 deletions internal/plugins/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/talwat/pap/internal/plugins/sources/paplug"
)

// Downloads a plugin.
func PluginDownload(plugin paplug.PluginInfo) {
for _, download := range plugin.Downloads {
var url string
Expand Down
3 changes: 3 additions & 0 deletions internal/plugins/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func GetPluginInfo(name string) paplug.PluginInfo {
return info
}

// Gets the information for a list of plugins.
// The plugins it gets the information for can also be dependencies or optional dependencies.
// checkInstalled will also verify if they are installed before returning information for them.
func GetManyPluginInfo(
plugins []string,
isDependencies bool,
Expand Down
Loading

0 comments on commit ee9f7d3

Please sign in to comment.