Skip to content

Commit

Permalink
feat: support IPFS_PATH, closes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Apr 1, 2018
1 parent 46ef477 commit 450c95b
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/articles/envvars.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ are used to control the behaviour of the library
| --- | --- |
| HOME | The user's home directory |
| HOMEPATH | same as `HOME` |
| IPFS_PATH | The folder of the IPFS [repository](repository) |
2 changes: 2 additions & 0 deletions doc/articles/repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

The repository is a persistent store for IPFS data. By default, it is located at `$HOME/.csipfs`. If not present, the [IPFS Engine](xref:Ipfs.Engine.IpfsEngine) will create it with the factory defaults.

To change its name and/or location use the [environment variables](envvars) or the [Repository Options](xref:Ipfs.Engine.IpfsEngine.RepositoryOptions)

A [SQLite database](https://sqlite.org/), named `ipfs.db`, stores most of the information. You can use [DB Browser](http://sqlitebrowser.org/) to examine and modify the information.
1 change: 1 addition & 0 deletions src/Cryptography/KeyChainOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Ipfs.Engine.Cryptography
/// <summary>
/// Configuration options for the <see cref="KeyChain"/>.
/// </summary>
/// <seealso cref="IpfsEngine.Options"/>
public class KeyChainOptions
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/IpfsEngineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Ipfs.Engine
/// <summary>
/// Configuration options for the <see cref="IpfsEngine"/>.
/// </summary>
/// <seealso cref="IpfsEngine.Options"/>
public class IpfsEngineOptions
{
/// <summary>
Expand Down
29 changes: 25 additions & 4 deletions src/RepositoryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,37 @@ namespace Ipfs.Engine
/// <summary>
/// Configuration options for the <see cref="Repository"/>.
/// </summary>
/// <seealso cref="IpfsEngineOptions"/>
public class RepositoryOptions
{
/// <summary>
/// Creates a new instance of the <see cref="RepositoryOptions"/> class
/// with the default values.
/// </summary>
public RepositoryOptions()
{
var path = Environment.GetEnvironmentVariable("IPFS_PATH");
if (path != null)
{
Folder = path;
}
else
{
Folder = Path.Combine(
Environment.GetEnvironmentVariable("HOME") ??
Environment.GetEnvironmentVariable("HOMEPATH"),
".csipfs");
}
}

/// <summary>
/// The directory of the repository.
/// </summary>
/// <value>
/// The default value is <c>$IPFS_PATH</c> or <c>$HOME/.csipfs</c> or
/// <c>$HOMEPATH/.csipfs</c>.
/// </value>
public string Folder { get; set; }
= Path.Combine(
Environment.GetEnvironmentVariable("HOME") ??
Environment.GetEnvironmentVariable("HOMEPATH"),
".csipfs");

/// <summary>
/// The fully qualified name of the database.
Expand Down
129 changes: 129 additions & 0 deletions test/RepositoryOptionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using Ipfs.Engine.Cryptography;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Ipfs.Engine
{

[TestClass]
public class RepositoryOptionsTest
{
[TestMethod]
public void Defaults()
{
var options = new RepositoryOptions();
Assert.IsNotNull(options.Folder);
Assert.IsNotNull(options.DatabaseName);
}

[TestMethod]
public void Folder()
{
var options = new RepositoryOptions { Folder = "x" };
var sep = Path.DirectorySeparatorChar;
Assert.AreEqual($"x{sep}ipfs.db", options.DatabaseName);
}

[TestMethod]
public void Environment_Home()
{
var names = new string[] { "IPFS_PATH", "HOME", "HOMEPATH" };
var values = names.Select(n => Environment.GetEnvironmentVariable(n));
var sep = Path.DirectorySeparatorChar;
try
{
foreach (var name in names)
{
Environment.SetEnvironmentVariable(name, null);
}
Environment.SetEnvironmentVariable("HOME", $"{sep}home1");
var options = new RepositoryOptions();
Assert.AreEqual($"{sep}home1{sep}.csipfs", options.Folder);
Assert.AreEqual($"{sep}home1{sep}.csipfs{sep}ipfs.db", options.DatabaseName);

Environment.SetEnvironmentVariable("HOME", $"{sep}home2{sep}");
options = new RepositoryOptions();
Assert.AreEqual($"{sep}home2{sep}.csipfs", options.Folder);
Assert.AreEqual($"{sep}home2{sep}.csipfs{sep}ipfs.db", options.DatabaseName);
}
finally
{
var pairs = names.Zip(values, (name, value) => new { name = name, value = value });
foreach (var pair in pairs)
{
Environment.SetEnvironmentVariable(pair.name, pair.value);
}
}
}

[TestMethod]
public void Environment_HomePath()
{
var names = new string[] { "IPFS_PATH", "HOME", "HOMEPATH" };
var values = names.Select(n => Environment.GetEnvironmentVariable(n));
var sep = Path.DirectorySeparatorChar;
try
{
foreach (var name in names)
{
Environment.SetEnvironmentVariable(name, null);
}
Environment.SetEnvironmentVariable("HOMEPATH", $"{sep}home1");
var options = new RepositoryOptions();
Assert.AreEqual($"{sep}home1{sep}.csipfs", options.Folder);
Assert.AreEqual($"{sep}home1{sep}.csipfs{sep}ipfs.db", options.DatabaseName);

Environment.SetEnvironmentVariable("HOMEPATH", $"{sep}home2{sep}");
options = new RepositoryOptions();
Assert.AreEqual($"{sep}home2{sep}.csipfs", options.Folder);
Assert.AreEqual($"{sep}home2{sep}.csipfs{sep}ipfs.db", options.DatabaseName);
}
finally
{
var pairs = names.Zip(values, (name, value) => new { name = name, value = value });
foreach (var pair in pairs)
{
Environment.SetEnvironmentVariable(pair.name, pair.value);
}
}
}

[TestMethod]
public void Environment_IpfsPath()
{
var names = new string[] { "IPFS_PATH", "HOME", "HOMEPATH" };
var values = names.Select(n => Environment.GetEnvironmentVariable(n));
var sep = Path.DirectorySeparatorChar;
try
{
foreach (var name in names)
{
Environment.SetEnvironmentVariable(name, null);
}
Environment.SetEnvironmentVariable("IPFS_PATH", $"{sep}x1");
var options = new RepositoryOptions();
Assert.AreEqual($"{sep}x1", options.Folder);
Assert.AreEqual($"{sep}x1{sep}ipfs.db", options.DatabaseName);

Environment.SetEnvironmentVariable("IPFS_PATH", $"{sep}x2{sep}");
options = new RepositoryOptions();
Assert.AreEqual($"{sep}x2{sep}", options.Folder);
Assert.AreEqual($"{sep}x2{sep}ipfs.db", options.DatabaseName);
}
finally
{
var pairs = names.Zip(values, (name, value) => new { name = name, value = value });
foreach (var pair in pairs)
{
Environment.SetEnvironmentVariable(pair.name, pair.value);
}
}
}

}
}

0 comments on commit 450c95b

Please sign in to comment.