path

The path module provides functions for working with file paths as strings. These functions operate purely on path strings without touching the filesystem.

import "conjure/path"

Path Components

base

Returns the base name (last component) of a path.

var filename = path.base("/home/user/document.txt")  // "document.txt"
var dirname = path.base("/home/user/")               // "user"
var simple = path.base("file.txt")                   // "file.txt"

Signature:

func base(path string) string

dir

Returns the directory component of a path. Returns "." if there is no directory component.

var parent = path.dir("/home/user/document.txt")  // "/home/user"
var root = path.dir("/foo")                       // "/"
var current = path.dir("document.txt")            // "."

Signature:

func dir(path string) string

ext

Returns the file extension including the dot. Returns an empty string if there is no extension.

var extension = path.ext("document.pdf")   // ".pdf"
var tarball = path.ext("archive.tar.gz")   // ".gz"
var noExt = path.ext("Makefile")           // ""
var hidden = path.ext(".gitignore")        // ""

Signature:

func ext(path string) string

Path Joining

join

Joins two path components with the appropriate separator. Handles trailing and leading slashes correctly.

var full = path.join("foo", "bar")      // "foo/bar"
var slash1 = path.join("foo/", "bar")   // "foo/bar"
var slash2 = path.join("foo", "/bar")   // "foo/bar"
var empty = path.join("", "bar")        // "bar"

Signature:

func join(a string, b string) string

Path Properties

isAbsolute

Returns true if the path is absolute (starts with /).

path.isAbsolute("/usr/bin")   // true
path.isAbsolute("./config")   // false
path.isAbsolute("")           // false

Signature:

func isAbsolute(path string) bool

isRelative

Returns true if the path is relative (does not start with /).

path.isRelative("./config")   // true
path.isRelative("src/main")   // true
path.isRelative("/usr/bin")   // false

Signature:

func isRelative(path string) bool

Extension Manipulation

withoutExt

Removes the extension from a path.

var noExt = path.withoutExt("document.txt")    // "document"
var partial = path.withoutExt("archive.tar.gz") // "archive.tar"
var unchanged = path.withoutExt("Makefile")     // "Makefile"

Signature:

func withoutExt(path string) string

replaceExt

Replaces the extension of a path with a new one. The new extension should include the dot if desired.

var newExt = path.replaceExt("document.txt", ".md")  // "document.md"
var addExt = path.replaceExt("README", ".txt")       // "README.txt"

Signature:

func replaceExt(path string, newExt string) string

Example: Build Output Path

import "conjure/path"
import "conjure/fs"

func getOutputPath(srcFile string) string {
    // src/main.cjr -> build/main.o
    var filename = path.base(srcFile)
    var objFile = path.replaceExt(filename, ".o")
    return path.join("build", objFile)
}

Example: Process Directory Files

import "conjure/path"
import "conjure/fs"
import "conjure/io"

func processSourceFiles(dir string) {
    // Process all .cjr files in a directory
    var files = fs.glob(path.join(dir, "*.cjr"))

    for file in files {
        var name = path.withoutExt(path.base(file))
        io.println("Processing: #{name}")
    }
}

Example: Safe Path Construction

import "conjure/path"
import "conjure/fs"

func resolveConfigPath(userPath string) string {
    // Convert relative paths to absolute
    if path.isRelative(userPath) {
        return path.join(fs.cwd(), userPath)
    }
    return userPath
}