diff options
| author | Nicolas Paul <n@nc0.fr> | 2023-10-01 23:03:40 +0200 |
|---|---|---|
| committer | Nicolas Paul <n@nc0.fr> | 2023-10-02 09:11:12 +0200 |
| commit | aff0b44f520aca6b1a77f6a195072dc10f69b8c8 (patch) | |
| tree | b0dccdcf4c276c9af31178cca2a9afe0c15f6bc0 | |
| parent | 608947dd5b47ac8e2a4ffe37cf2cfcd628f10f00 (diff) | |
Flatten structure
Related to #13
| -rw-r--r-- | pkg/types/index.go | 113 | ||||
| -rw-r--r-- | prelude.go (renamed from pkg/config/lib/prelude/prelude.go) | 23 | ||||
| -rw-r--r-- | starlark.go (renamed from pkg/config/starlark.go) | 21 | ||||
| -rw-r--r-- | svgu.go (renamed from cmd/svgu/svgu.go) | 11 | ||||
| -rw-r--r-- | templates.go (renamed from pkg/templates/templates.go) | 4 | ||||
| -rw-r--r-- | templates_test.go (renamed from pkg/templates/templates_test.go) | 2 | ||||
| -rw-r--r-- | types.go (renamed from pkg/types/module.go) | 72 |
7 files changed, 100 insertions, 146 deletions
diff --git a/pkg/types/index.go b/pkg/types/index.go deleted file mode 100644 index 3abf527..0000000 --- a/pkg/types/index.go +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright Nicolas Paul (2023) -// -// * Nicolas Paul -// -// This software is a computer program whose purpose is to allow the hosting -// and sharing of Go modules using a personal domain. -// -// This software is governed by the CeCILL license under French law and -// abiding by the rules of distribution of free software. You can use, -// modify and/ or redistribute the software under the terms of the CeCILL -// license as circulated by CEA, CNRS and INRIA at the following URL -// "http://www.cecill.info". -// -// As a counterpart to the access to the source code and rights to copy, -// modify and redistribute granted by the license, users are provided only -// with a limited warranty and the software's author, the holder of the -// economic rights, and the successive licensors have only limited -// liability. -// -// In this respect, the user's attention is drawn to the risks associated -// with loading, using, modifying and/or developing or reproducing the -// software by the user in light of its specific status of free software, -// that may mean that it is complicated to manipulate, and that also -// therefore means that it is reserved for developers and experienced -// professionals having in-depth computer knowledge. Users are therefore -// encouraged to load and test the software's suitability as regards their -// requirements in conditions enabling the security of their systems and/or -// data to be ensured and, more generally, to use and operate it in the -// same conditions as regards security. -// -// The fact that you are presently reading this means that you have had -// knowledge of the CeCILL license and that you accept its terms. - -package types - -import ( - "go.nc0.fr/svgu/pkg/templates" - "os" - "path" - "sync" -) - -// Index is the global object representing the Starlark configuration. -type Index struct { - Domain string - Modules map[string]*Module - // internal - lock sync.Mutex -} - -// SetDomain sets the domain of the index. -func (i *Index) SetDomain(d string) { - i.lock.Lock() - defer i.lock.Unlock() - i.Domain = d -} - -// AddModule adds a module to the index. -func (i *Index) AddModule(n string, m *Module) { - i.lock.Lock() - defer i.lock.Unlock() - i.Modules[n] = m -} - -// GetModule returns a module from the index. -func (i *Index) GetModule(n string) *Module { - i.lock.Lock() - defer i.lock.Unlock() - return i.Modules[n] -} - -// RemoveModule removes a module from the index. -func (i *Index) RemoveModule(n string) { - i.lock.Lock() - defer i.lock.Unlock() - delete(i.Modules, n) -} - -// CheckModule checks if a module is in the index. -func (i *Index) CheckModule(n string) bool { - i.lock.Lock() - defer i.lock.Unlock() - _, ok := i.Modules[n] - return ok -} - -// GenerateFile generates the index file. -func (i *Index) GenerateFile(out string) error { - i.lock.Lock() - defer i.lock.Unlock() - - f := path.Join(out, "index.html") - - // Create the file. - fd, err := os.Create(f) - if err != nil { - return err - } - defer func(fd *os.File) { - err := fd.Close() - if err != nil { - panic(err) - } - }(fd) - - // Execute the template and write the output to the file. - if err := templates.ExecIndex(fd, - "https://pkg.go.dev", 2); err != nil { - return err - } - - return nil -} diff --git a/pkg/config/lib/prelude/prelude.go b/prelude.go index 16f9f4d..d109a82 100644 --- a/pkg/config/lib/prelude/prelude.go +++ b/prelude.go @@ -31,11 +31,12 @@ // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. -package prelude +// Prelude for the Starlark environment. + +package main import ( "fmt" - "go.nc0.fr/svgu/pkg/types" "go.starlark.net/starlark" "strings" ) @@ -44,7 +45,7 @@ import ( const invalidName string = "..\\/<>:\"|?* \t\n\r\b\findex" // Registered represents the index of registered modules. -var Registered types.Index +var Registered Index // InternIndex represents the built-in function "index". // index(domain) initializes a new index with the given domain. @@ -56,7 +57,7 @@ func InternIndex(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, return nil, err } - Registered.SetDomain(domain) + Registered.Domain = domain return starlark.None, nil } @@ -98,23 +99,23 @@ func InternModule(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, return nil, fmt.Errorf("module %q already exists", name) } - var v types.Vcs + var v Vcs switch vcs { case "git": - v = types.VcsGit + v = VcsGit case "hg": - v = types.VcsMercurial + v = VcsMercurial case "svn": - v = types.VcsSubversion + v = VcsSubversion case "fossil": - v = types.VcsFossil + v = VcsFossil case "bzr": - v = types.VcsBazaar + v = VcsBazaar default: return nil, fmt.Errorf("unknown vcs %q", vcs) } - Registered.AddModule(name, &types.Module{ + Registered.AddModule(name, &Module{ Path: name, Vcs: v, Repo: repo, diff --git a/pkg/config/starlark.go b/starlark.go index bb65465..9f79a69 100644 --- a/pkg/config/starlark.go +++ b/starlark.go @@ -31,12 +31,11 @@ // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. -package config +// Loader for the Starlark execution system with a pre-defined environment. + +package main import ( - "fmt" - "go.nc0.fr/svgu/pkg/config/lib/prelude" - "go.nc0.fr/svgu/pkg/types" "go.starlark.net/starlark" ) @@ -44,23 +43,23 @@ import ( // configuration file "fl". // The function returns a list of registered modules, or an error if something // went wrong. -func ExecConfig(fl string) (*types.Index, error) { +func ExecConfig(fl string) (*Index, error) { th := &starlark.Thread{ Name: "exec " + fl, } env := starlark.StringDict{ - "index": starlark.NewBuiltin("index", prelude.InternIndex), - "module": starlark.NewBuiltin("module", prelude.InternModule), + "index": starlark.NewBuiltin("index", InternIndex), + "module": starlark.NewBuiltin("module", InternModule), } - prelude.Registered = types.Index{ + Registered = Index{ Domain: "", - Modules: make(map[string]*types.Module), + Modules: make(map[string]*Module), } if _, err := starlark.ExecFile(th, fl, nil, env); err != nil { - return &types.Index{}, err + return &Index{}, err } - return &prelude.Registered, nil + return &Registered, nil } diff --git a/cmd/svgu/svgu.go b/svgu.go index 1b66db0..3a2e775 100644 --- a/cmd/svgu/svgu.go +++ b/svgu.go @@ -31,7 +31,9 @@ // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. -// The svgu tool. +// Entry-point of the CLI program. + +// Package main implements the SVGU utility. package main // import "go.nc0.fr/svgu" import ( @@ -40,9 +42,6 @@ import ( "os" "path/filepath" "sync" - - "go.nc0.fr/svgu/pkg/config" - "go.nc0.fr/svgu/pkg/types" ) var ( @@ -91,7 +90,7 @@ func main() { log.Printf("executing configuration file %q", *cfg) } - idx, err := config.ExecConfig(*cfg) + idx, err := ExecConfig(*cfg) if err != nil { log.Fatalf("could not execute configuration file %q: %v", *cfg, err) } @@ -123,7 +122,7 @@ func main() { var mu sync.Mutex for _, mod := range idx.Modules { wg.Add(1) - go func(m *types.Module) { + go func(m *Module) { defer wg.Done() defer mu.Unlock() diff --git a/pkg/templates/templates.go b/templates.go index 4c227a5..e12413e 100644 --- a/pkg/templates/templates.go +++ b/templates.go @@ -31,7 +31,9 @@ // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. -package templates +// HTML templates to generate for each module registered in the index. + +package main import ( "fmt" diff --git a/pkg/templates/templates_test.go b/templates_test.go index 34c2c82..ac386e1 100644 --- a/pkg/templates/templates_test.go +++ b/templates_test.go @@ -31,7 +31,7 @@ // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. -package templates +package main import ( "bytes" diff --git a/pkg/types/module.go b/types.go index 2206659..28651b3 100644 --- a/pkg/types/module.go +++ b/types.go @@ -31,17 +31,83 @@ // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. -package types +// Data structure representaton of the configuration + +package main import ( "fmt" - "go.nc0.fr/svgu/pkg/templates" "os" "path" "strings" "sync" ) +// Index is the global registry of Go modules. +type Index struct { + Domain string + Modules map[string]*Module + // internal + lock sync.Mutex +} + +// AddModule adds a module to the index. +func (i *Index) AddModule(n string, m *Module) { + i.lock.Lock() + defer i.lock.Unlock() + i.Modules[n] = m +} + +// GetModule returns a module from the index. +func (i *Index) GetModule(n string) *Module { + i.lock.Lock() + defer i.lock.Unlock() + return i.Modules[n] +} + +// RemoveModule removes a module from the index. +func (i *Index) RemoveModule(n string) { + i.lock.Lock() + defer i.lock.Unlock() + delete(i.Modules, n) +} + +// CheckModule checks if a module is in the index. +func (i *Index) CheckModule(n string) bool { + i.lock.Lock() + defer i.lock.Unlock() + _, ok := i.Modules[n] + return ok +} + +// GenerateFile generates the index file. +func (i *Index) GenerateFile(out string) error { + i.lock.Lock() + defer i.lock.Unlock() + + f := path.Join(out, "index.html") + + // Create the file. + fd, err := os.Create(f) + if err != nil { + return err + } + defer func(fd *os.File) { + err := fd.Close() + if err != nil { + panic(err) + } + }(fd) + + // Execute the template and write the output to the file. + if err := ExecIndex(fd, + "https://pkg.go.dev", 2); err != nil { + return err + } + + return nil +} + // Vcs is an enum for version control systems supported by the standard Go // toolchain. // @@ -100,7 +166,7 @@ func (m *Module) GenerateFile(out string, domain string) error { }(fd) // Execute the template and write the output to the file. - if err := templates.ExecModule(fd, + if err := ExecModule(fd, fmt.Sprintf("%s/%s", domain, p), string(v), r, d, f); err != nil { return err |
